Rscript ggplot - ggsave 问题

发布于 2024-11-27 01:13:15 字数 1205 浏览 0 评论 0原文

我使用以下代码绘制图形并将其打印为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

起风了 2024-12-04 01:13:15

问题是调用 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

--vanilla 基本上意味着 Rscript 在开始执行 R 之前应该忘记所有内容命令,包括一些已安装的软件包。

The problem was the --vanilla option when calling Rscript.

Here is what Rscript --help says about its options

--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 basically means that Rscriptshould forget everything before it starts executing R commands, including some of the installed packages.

囍孤女 2024-12-04 01:13:15

as.formula 位于 stats 包中。不确定为什么它没有被加载,但看看在脚本开头手动包含 library(stats) 是否可以解决该问题。

as.formula is in the stats package. Not sure why it isn't being loaded, but see if manually including library(stats) at the start of your script fixes the issue.

心作怪 2024-12-04 01:13:15

两件事:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文