ecjb
ecjb

Reputation: 5449

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

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?

Upvotes: 2

Views: 11397

Answers (2)

Parfait
Parfait

Reputation: 107567

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()

Upvotes: 1

Sada93
Sada93

Reputation: 2835

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)

Upvotes: 8

Related Questions