Rscript ggplot - ggsave 问题
我使用以下代码绘制图形并将其打印为 PDF:
ggplot(agg_data_cl, aes( as.Date(date), avg_entries, color = classification ) ) +
geom_point()
ggsave(file = "output.pdf")
当我在 TextMate 中执行脚本时,效果很好。
但是,当我通过 RScript 从命令行调用它时,出现以下错误:
Error in all.vars(as.formula(.$facets)) :
could not find function "as.formula"
Calls: print ... <Anonymous> -> <Anonymous> -> <Anonymous> -> all.vars
Execution halted
我在 RScrpipt 文件中使用以下标头:
#! /usr/local/bin/Rscript --vanilla --default-packages=utils
有什么想法可能是什么问题吗?
这是我的命令行会话信息:
R version 2.13.1 (2011-07-08)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] C/UTF-8/C/C/C/C
attached base packages:
[1] grid utils base
other attached packages:
[1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.5.2
loaded via a namespace (and not attached):
[1] grDevices_2.13.1 graphics_2.13.1 stats_2.13.1
在 textmate sessionInfo() 中为我提供了更多附加的基础包:
attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base
我不知道为什么会出现这种情况。
I am using the following code to draw a graph and print it to a PDF:
ggplot(agg_data_cl, aes( as.Date(date), avg_entries, color = classification ) ) +
geom_point()
ggsave(file = "output.pdf")
This works fine when I execute the script in TextMate.
But when I call it from the commandline via RScript I get the following error:
Error in all.vars(as.formula(.$facets)) :
could not find function "as.formula"
Calls: print ... <Anonymous> -> <Anonymous> -> <Anonymous> -> all.vars
Execution halted
I use the following header in my RScrpipt file:
#! /usr/local/bin/Rscript --vanilla --default-packages=utils
Any ideas what could be the problem?
Here is my command line session information:
R version 2.13.1 (2011-07-08)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] C/UTF-8/C/C/C/C
attached base packages:
[1] grid utils base
other attached packages:
[1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.5.2
loaded via a namespace (and not attached):
[1] grDevices_2.13.1 graphics_2.13.1 stats_2.13.1
In textmate sessionInfo() gives me more attached base packages:
attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base
I have no idea why this the case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是调用
Rscript
时的--vanilla
选项。以下是
Rscript --help
关于其选项的说明。
--vanilla
基本上意味着Rscript
在开始执行 R 之前应该忘记所有内容命令,包括一些已安装的软件包。The problem was the
--vanilla
option when callingRscript
.Here is what
Rscript --help
says about its options..and
--vanilla
basically means thatRscript
should forget everything before it starts executing R commands, including some of the installed packages.as.formula
位于 stats 包中。不确定为什么它没有被加载,但看看在脚本开头手动包含library(stats)
是否可以解决该问题。as.formula
is in the stats package. Not sure why it isn't being loaded, but see if manually includinglibrary(stats)
at the start of your script fixes the issue.两件事:
1)确保你有库(ggplot2)_in_your_script_
2)确保你 print() 或plot() ggplot 对象。 (请参阅常见问题解答 7.22)在这种情况下,这可能不是问题,因为您正在节省 gg,但这是 Rscript 会话中的点阵或 ggplot 图形的常见问题。
Two things:
1) Make sure you have library(ggplot2) _in_your_script_
2) Make sure you print() or plot() the ggplot object. (See the FAQ 7.22) This may not be the problem in this instance, since you are gg-saving, but it is a common problem with lattice or ggplot graphics from Rscript sessions.