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?
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)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With