2011-07-29 72 views
1

我使用下面的代碼來畫圖,打印到PDF:RSCRIPT ggplot - ggsave問題

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 

我不知道爲什麼這樣的情況。

+0

請發佈您的:'sessionInfo()' –

回答

3

as.formula在統計信息包中。不知道爲什麼它沒有被加載,但看看在腳本的開始手動包括library(stats)是否可以修復問題。

+0

是的,問題是有些軟件包在作爲腳本運行時沒有加載 – behas

3

兩件事情:

1)請確保您有庫(GGPLOT2)_in_your_script_

2)確保您打印()或情節()的ggplot對象。 (請參閱常見問題解答7.22)在這種情況下,這可能不是問題,因爲您節省了gg,但它是來自Rscript會話的格或ggplot圖形的常見問題。

+0

+1(ggplot2),這也是我的第一個想法。 –

+2

我最初的想法是'print()'。 :) –

4

問題是撥打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基本上意味着它開始執行R命令,包括一些已安裝的軟件包之前Rscript應該忘記一切。