在R环境中工作的图,但不是通过RScript

时间:2016-11-17 09:30:29

标签: r plot rscript

任何人都可以解释为什么我从命令行运行它:

>R #So I'm in the R environment
>plot(cars)
>sessionInfo()

绘图按预期生成,sessionInfo的输出为:

> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu precise (12.04.5 LTS)

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C  

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base  

但是,当我将两行代码放入一个名为Test.R的脚本中时,它看起来像这样:

#!/usr/bin/Rscript
plot(cars)
sessionInfo()

运行“Rscript Test.r”

没有生成情节,会话信息的输出为:

R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu precise (12.04.5 LTS)

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  base   

两个sessionInfo的外观相同,我使用“-X”参数登录到终端,输入“xclock”会生成一个时钟图片,之前我使用过Rscript来生成图表。当我阅读可能有用的here时,我还添加了一行“require(methods)”。如果有人可以提供帮助,我可以做些什么来查明问题,我会很感激。

编辑1:回复Amit的评论如下: 我实际上是在Mac上使用unix,我认为你的方法是针对windows的,所以我稍微编辑了一下我认为应该是的命令,但如果我做错了,请告诉我。我这样做了:

$ /usr/bin/Rscript --vanilla --slave Test2.r 
Loading required package: methods
R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu precise (12.04.5 LTS)

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] methods   stats     graphics  grDevices utils     datasets  base 

1 个答案:

答案 0 :(得分:-1)

如果我像这样直接打印到PDF,我可以得到要打印的情节:

#!/bin/Rscript
require(methods)
pdf("cars.pdf")
plot(cars)
dev.off()