Ricky
Ricky

Reputation: 4686

Troubleshooting R script called from Rscript.exe

I have an R script that runs fine in RStudio. I am trying to schedule it to run regularly in Windows Task Scheduler with RScript.exe. It failed.

Then I tried running it from command line and noticed an error below.

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

When I sourced directly in RStudio for the same script, it worked perfectly fine.

Any tips on how I can investigate and troubleshoot this?

R session info below in case of use:

> 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 

Upvotes: 3

Views: 2741

Answers (2)

CMStandish
CMStandish

Reputation: 11

I used cmd to call Rscript.exe in Task Scheduler, then gave it an absolute path to my R script, which further defined the output to an rmarkdown file. I should note that I also had to install pandoc on my computer to get it to work (https://pandoc.org/installing.html). But I now have a rmarkdown report which runs every day, and draws data directly from an MSAccess database to provide me with a summary I can send my boss and staff.

Code:

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

in run_report.R I specify the output:

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)

Upvotes: 1

Ricky
Ricky

Reputation: 4686

After some further searching I found two ways to make this work:

  1. Instead of using Rscript.exe, I force the input into R.exe. i.e. instead of Rscript.exe batch_read_rss_selected.R earlier, I usedR.exe -f batch_read_rss_selected.R. This worked, and dumped every single line of code in the script on screen also.
  2. Change the script to explicitly include library(methods) . It seems this is not needed for the script to function when running from within R (RStudio IDE in my case), but is needed for Rscript.exe for my script. Now it executes similarly both on command line and from R environment.

Upvotes: 7

Related Questions