【问题标题】:Troubleshooting R script called from Rscript.exe从 Rscript.exe 调用的 R 脚本故障排除
【发布时间】:2015-12-18 02:47:46
【问题描述】:

我有一个在 RStudio 中运行良好的 R 脚本。我正在尝试使用 RScript.exe 安排它在 Windows 任务计划程序中定期运行。失败了。

然后我尝试从命令行运行它并注意到下面的错误。

D:\development\projects\SSCDAPOC\trunk\src\scripts>"C:\Program Files\R\R-3.2.2\bin\x64\Rscript.exe" batch_read_rss_selected.R
Loading required package: xml2
Error in as.vector(x, "list") :
  cannot coerce type 'environment' to vector of type 'list'
Calls: do.call ... <Anonymous> -> lapply -> as.list -> as.list.default
Execution halted

当我直接在 RStudio 中获取相同的脚本时,它工作得非常好。

关于如何调查和解决此问题的任何提示?

以下R会话信息以备使用:

> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_Singapore.1252  LC_CTYPE=English_Singapore.1252   
[3] LC_MONETARY=English_Singapore.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Singapore.1252    

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

other attached packages:
[1] rvest_0.3.1 xml2_0.1.2 

loaded via a namespace (and not attached):
 [1] httr_1.0.0    selectr_0.2-3 R6_2.1.1      magrittr_1.5  tools_3.2.2   curl_0.9.4   
 [7] Rcpp_0.12.2   stringi_1.0-1 stringr_1.0.0 XML_3.98-1.3 

【问题讨论】:

    标签: r


    【解决方案1】:

    经过进一步搜索,我找到了两种方法来完成这项工作:

    1. 我没有使用 Rscript.exe,而是强制输入到 R.exe。即代替 Rscript.exe batch_read_rss_selected.R 之前,我用过R.exe -f batch_read_rss_selected.R。这很有效,并且脚本中的每一行代码也都转储到屏幕上。
    2. 更改脚本以明确包含 library(methods) 。从 R (在我的情况下为 RStudio IDE)中运行时,脚本似乎不需要此功能,但对于我的脚本,Rscript.exe 需要此功能。现在它在命令行和 R 环境中的执行方式相似。

    【讨论】:

      【解决方案2】:

      我在任务计划程序中使用cmd 调用Rscript.exe,然后给它一个我的R 脚本的绝对路径,它进一步定义了输出到一个rmarkdown 文件。我应该注意,我还必须在我的计算机上安装 pandoc 才能使其工作 (https://pandoc.org/installing.html)。但是我现在有一个每天运行的 rmarkdown 报告,它直接从 MSAccess 数据库中提取数据,为我提供一个摘要,我可以发送给我的老板和员工。

      代码:

      program/script = cmd
      additional argument =
      /c C:/<yourpath>/Rscript.exe "C:/<yourpath>/run_report.R"
      

      在 run_report.R 中我指定了输出:

      rmarkdown::render("C:/<yourpath>/filename.Rmd", output_format= NULL,output_file="C:/yourpath>/filename.pdf", knit_root_dir=NULL, runtime="auto",  clean=TRUE, run_pandoc = TRUE, quiet=TRUE)
      

      【讨论】:

      • 感谢编辑!这是我的第一篇文章。 CMStandish
      猜你喜欢
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 2010-11-30
      相关资源
      最近更新 更多