Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a plot in r in a script from the terminal

Tags:

terminal

plot

r

It is possible to run a r script from the terminal:

Let's say I have a file "message.r" which contains the following:

print("hello world")

I can run the script from the terminal with the the following command:

$ Rscript message.r
[1] "hello world"

Let's says now that I have a script containg code for a plot names plot.r with the following content:

x = c(1,2,3)
y = c(2,3,6)
plot(x,y)

Entering the command

Rscript plot.r

nothing happens

How to make display a plot from the terminal?

like image 968
ecjb Avatar asked Dec 14 '25 18:12

ecjb


2 Answers

You need to set up a device driver. This saves the plot to the desktop.

x = c(1,2,3)
y = c(2,3,6)

pdf("~/Desktop/img.pdf")
plot(x,y)
dev.off()

system('open ~/Desktop/img.pdf')

Or directly onto the terminal window,

library(txtplot)
x = c(1,2,3)
y = c(2,3,6)

txtplot(x,y)

like image 59
Sada93 Avatar answered Dec 17 '25 13:12

Sada93


Consider launching an R session in terminal (which by default loads graphics and grDevices libraries and others including base, utils, stats etc.).

Then, source() your script which will run base plots and launch needed plot window to screen. At the end, quit session with q() as needed.

> R.exe
> source("myPlot.r")
> q()
like image 26
Parfait Avatar answered Dec 17 '25 12:12

Parfait



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!