我正在使用以下代码绘制图形并将其打印为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我不知道为什么会这样。
发布于 2011-07-29 17:52:03
as.formula在stats包中。不确定为什么没有加载它,但看看在脚本开头手动包含library(stats)是否解决了这个问题。
发布于 2011-07-29 23:42:36
问题出在调用Rscript时的--vanilla选项。
以下是Rscript --help对其选项的看法
--save Do save workspace at the end of the session
--no-environ Don't read the site and user environment files
--no-site-file Don't read the site-wide Rprofile
--no-init-file Don't read the user R profile
--restore Do restore previously saved objects at startup
--vanilla Combine --no-save, --no-restore, --no-site-file
--no-init-file and --no-environ..and --vanilla基本上意味着Rscript应该在开始执行R命令之前忘记所有东西,包括一些已安装的软件包。
发布于 2011-07-29 10:40:51
两件事:
1)确保拥有库(Ggplot2) _in_your_script_
2)确保对ggplot对象执行print()或plot()操作。(参见FAQ 7.22)在这种情况下,这可能不是问题,因为您正在保存gg,但这是来自Rscript会话的晶格或ggplot图形的常见问题。
https://stackoverflow.com/questions/6867996
复制相似问题