Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

r - How to pop up the graphics window from Rscript?

I'm using Rscript to run a plot as follows:

x=1:10 
y=1:10 
plot(x,y) 

I expect this code to popup a window with a graphic showing the plot when I run the code like this:

Rscript plot.R

The program finishes to completion, and the graphic does not appear, not even momentarily. I know this code is right because it does produce a plot in the Rstudio GUI.

Does Rscript have a feature to popup that plot automatically on execution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Pop up the graphics window from Rscript, Example 1:

library(tcltk)
x=1:10 
y=1:10 
windows()     #Use X11() or quartz() if on linux or mac.
plot(x,y)
prompt  <- "hit spacebar to close plots"
extra   <- "some extra comment"
capture <- tk_messageBox(message = prompt, detail = extra)

The above code presents the plot in a popup window and waits for you to press ok on the tk_messageBox dialog.

Pop up the graphics window from Rscript, Example 2:

png("mygraphic.png") 
x = 1:10
print(x^2)
plot(x, x^2, 'o')
print("done")
dev.off()
browseURL("mygraphic.png") 

The above code saves the png to disk as a file and asks the operating system to open the file in the program designed for that filetype.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...