Rscript 正在绘制为 PDF [英] Rscript is plotting to PDF

查看:49
本文介绍了Rscript 正在绘制为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 R 脚本.当它通过 Rscript.exe 运行时,默认情况下它会打印为 PDF 文件.我希望脚本打开一个绘图窗口.

I've a simple R script. When it is run via Rscript.exe, by default it is plotting to a PDF file. I want the script to open a plot window.

我使用的命令:

Rscript.exe tmp_plot.R

r 文件 tmp_plot.R 包含:

r file tmp_plot.R contains:

x <- 1:10
y <- sin(x)
plot(x,y)

推荐答案

您以非交互式方式运行 R - Rscript 用于脚本 - 因此默认绘图设备是 pdf(),而不是 x11() 或任何您操作系统的默认值(windows() 从外观上看).然而,打开替代设备是微不足道的.使用 x11()windows().您在尝试编写将在屏幕上显示绘图的脚本时遇到的问题是,在您显示的示例代码中,脚本在绘制绘图时立即终止,无论是显示在屏幕上还是显示在 pdf()代码>设备.充其量你可以使用 Sys.sleep() 让它暂停,例如:

You are running R in a non-interactive way - Rscript is meant for scripts - hence the default plotting device is pdf(), not x11() or whatever is your OS's default (windows() by the looks of it). It is trivial to open an alternative device, however; use x11() or windows(). The problem you have in trying to write a script that will display a plot on screen is that, in your example code shown, the script terminates immediately upon drawing the plot, whether displayed on screen or on the pdf() device. At best you might get it to pause using Sys.sleep(), e.g.:

x <- 1:10
y <- sin(x)
x11() ## or windows()
plot(x,y)
Sys.sleep(10)

我认为你这样做是错误的.如果您在运行 R脚本"时想要交互性,我的意思是一组执行某些分析的 R 语句,您最好在您的操作系统上安装一个编辑器/IDE,它允许您单步执行脚本一行或一次一段代码,加上与正在运行的 R 会话进行交互.我为此使用 Emacs 和 ESS 扩展.您可以考虑将 Tinn-R 或 RStudio 作为替代方案.

I think you are going about this the wrong way. If you want interactivity when running an R "script", by which I mean a set of R statements that perform some analysis, you'd be better off getting an editor/IDE on your OS that allows you to step through the script a line or chunk of code at a time, plus interact with the running R session. I use Emacs and the ESS extension for this. You might consider Tinn-R or RStudio as alternatives.

Rscript 用于运行不需要人工交互或干预的脚本或类似批处理的作业.

Rscript is meant for running scripting or batch-like jobs that do not need human interaction or intervention.

这篇关于Rscript 正在绘制为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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