如何从Rscript弹出图形窗口? [英] How to pop up the graphics window from Rscript?

查看:168
本文介绍了如何从Rscript弹出图形窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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

程序完成,并且图形没有出现,甚至没有出现.我知道这段代码是正确的,因为它确实在Rstudio GUI中产生了一个图.

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.

Rscript是否具有弹出功能,可在执行时自动绘图?

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

推荐答案

从Rscript弹出图形窗口,示例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)

上面的代码在一个弹出窗口中显示该图,并等待您在tk_messageBox对话框中按OK.

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

从Rscript弹出图形窗口,示例2:

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

上面的代码将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.

这篇关于如何从Rscript弹出图形窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆