Troubleshooting R script called from Rscript.exe - r

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

After some further searching I found two ways to make this work:
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.
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.

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)

Related

Can't install TinyTeX on Windows Server

This is similar to this issue, but the solutions mentioned didn't work for me. I tried to install TinyTeX via different methods:
tinytex::install_tinytex()
download TinyTeX-0.zip, and use tinytex:::install_prebuilt() on it
manually extract TinyTeX-0.zip (unzip() works) and run install-tl-windows (Batch file)
In the first two cases, I have this error:
Error in system2("tlmgr", args, ...) : '"tlmgr"' not found
In addition: Warning message:
In extract(pkg, exdir = path.expand(target)) :
error 1 in extracting from zip file
In the third case, the Windows console looks like this:
'\\crc\users\etienne\Desktop\TinyTeX-0\TinyTeX'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
and then closes without doing anything.
In every case, tinytex::is_tinytex() returns FALSE and tinytex::tlmgr_path() returns nothing.
I am working on Windows Server 2019 Standard (1809). What should I do?
> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252
[3] LC_MONETARY=French_France.1252 LC_NUMERIC=C
[5] LC_TIME=French_France.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.1.0 tools_4.1.0 tinytex_0.33.1 xfun_0.25
Solved in this GitHub issue
In summary:
run tinytex::install_tinytex(dir = "<path>") where <path> is a local folder on the server, e.g in my case "C:/Users/etienne/TinyTeX"
put options(tinytex.tlmgr.path = tinytex:::find_tlmgr("<path>")) in your .Rprofile (you can easily do that with usethis::edit_r_profile("user"))
restart R
check that tinytex::is_tinytex() is TRUE (you can also try to install a package with tinytex::tlmgr_install("booktabs") for instance)

R refers to non exisiting files during R CMD check

I want to R CMD check with RStudio my package (for this question I call it pkg). But I get the following error message:
* preparing 'pkg':
* checking DESCRIPTION meta-information ... OK
* installing the package to build vignettes
* creating vignettes ...Error in find_vignette_product(name, by = "weave", dir = docdir, engine = engine) :
Failed to locate the 'weave' output file (by engine 'utils::Sweave') for vignette with name 'my-vignette'. The following files exist in directory 'C:/Users/name/AppData/Local/Temp/RtmpQLnSjE/Rbuild244434d45c05/pkg/vignettes': 'my-vignette.R', 'my-vignette.Rmd'
Execution halted
Error: Command failed (1)
In addition: Warning message:
`cleanup` is deprecated
Execution halted
Exited with status 1.
One first thing, where something might got wrong is that there is no folder called RtmpQLnSjE in my C:/Users/name/AppData/Local/Temp/ directory. Running my Rmd file by hand, does not generate any errors.
I update RStudio to the latest version 1.1.423 and this is my SessionInfo():
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C LC_TIME=German_Germany.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] MCMCglmm_2.25 ape_5.0 coda_0.19-1 lme4_1.1-15 Rcpp_0.12.15 Matrix_1.2-12 hmi_0.9.4
loaded via a namespace (and not attached):
[1] lattice_0.20-35 corpcor_1.6.9 digest_0.6.15 withr_2.1.1 MASS_7.3-48 grid_3.4.3
[7] nlme_3.1-131 cubature_1.3-11 minqa_1.2.4 nloptr_1.0.4 devtools_1.13.4 splines_3.4.3
[13] tools_3.4.3 yaml_2.1.16 parallel_3.4.3 compiler_3.4.3 memoise_1.1.0 tensorA_0.36
Concluding as an answer:
Failed to locate the 'weave' output file
This error can have several root causes and is not always easy to trace down. However, there is a good chance that it is related to code inside the vignette, in particular code navigating directories and such (e.g. setwd() or other functions that may rely on relative paths that could differ between a stand-alone run and the more involved R CMD check)
I recently ran across this when a vignette file had a space in the name (like "My Vignette.Rmd"). It was creating an html file called My-Vignette.html. I think it's the hyphen in the name that was causing issues (based on the answer above along with what I'm seeing.)

R Error in validObject(.Object) when running as script, but not in console

The following code works fine in the R console (R 3.3.0):
m = system.file("external/pores_1.mtx", package = "Matrix")
x = Matrix::readMM(m)
I can put it in a script and it runs fine in R console as:
source("test.R")
However, when I execute it as Rscript --vanilla test.R or Rscript test.R, I get an error:
Error in validObject(.Object) :
invalid class “dgTMatrix” object: Not a valid 'Mnumeric' class object
Calls: <Anonymous> -> new -> initialize -> initialize -> validObject
Execution halted
I don't know if this is related to that specific function. I am guessing this has something to do with how Rscript works, but I used it with various other libraries and functions previously and have never seen anything like this. Any idea what is happening?
I can confirm ... I'm getting the exact same error when running a script containing a call to glmnet(). I was able to trace it back to the Matrix package, on which glmnet depends.
I back-rev'd my R version from v3.3.3 -> v3.3.2 and the error disappeared. I then checked the sessionInfo() between the two and discovered that the version of the Matrix package differed ... it is 1.2-8 (in v3.3.3) and 1.2-7.1 (in v3.3.2). To confirm, I simply replaced the "OK" version of Matrix (7.1) with the "broken" version and the error returned.
I can also confirm that explicitly loading the methods package via library(methods) resolves the error (somehow?), which explains the differing behavior between the console call and the Rscript call from the command line.
So, it appears we have 2 work-arounds:
library(methods)
back rev your version of Matrix to 1.2-7.1.
Neither is super satisfactory ... I'd just like to know what's going on with Matrix 1.2-8. Maybe it'll be bug-fixed in the next version.
If you're interested, here is my sessionInfo():
R version 3.3.3 (2017-03-06)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets base
other attached packages:
[1] glmnet_2.0-5 foreach_1.4.3 Matrix_1.2-8
loaded via a namespace (and not attached):
[1] codetools_0.2-15 grid_3.3.3 iterators_1.0.8 methods_3.3.3
[5] lattice_0.20-35
Oh dear.
I'm pretty (not 100% !) sure that this problem should not apply in newer version of R and Matrix.
Still I would claim this is not a Matrix bug in a proper sense, rather either an 'Rscript' vs 'R' bug -- as #Stu Field mentions already; Rscript does not by default attach the methods package to the search() path, but regular R does.
OTOH, R CMD check Matrix nowadays should try to run Matrix without the methods package being in the search path, and hence the problem should not be present from Matrix 1.2-9 and newer, i.e., by default from R 3.4.0 and newer or then update the 'Matrix' package if you have an older version of R.
Again: Can you please confirm that the problem is gone with R 3.4.0 (which has Matrix 1.2-9 "along with it") ?
Here is a more useful example script, I called Rscript-tst.R.
Run as
Rscript --vanilla Rscript-tst.R > Rscript-tst.Rout 2>&1
or (as myself with many R versions installed) something like
`R-3.4.0 RHOME`/bin/Rscript --vanilla Rscript-tst.R > Rscript-tst.Rout_R340 2>&1
Rscript-tst.R :
options(echo = TRUE)# << even with "Rscript" or --slave ...
(m <- system.file("external/pores_1.mtx", package = "Matrix"))
packageDescription("Matrix")
## This *load*s the Matrix package but does not attach it to search()
str(Matrix::readMM)
sessionInfo()
x <- Matrix::readMM(m)
## used to fail because 'methods' was not "there" (in Rscript only)

Where are the fix() and edit() functions in RStudio Server?

I followed the official guide to install RStudio Server. Everything works fine, but the functions, fix and edit, are not available.
For instance, I have a data.frame called "data" as follows.
1,2,3
4,5,6
7,8,9
The command "View(data)" works fine. But when I execute the command "edit(data)" or "fix(data)", it comes some error messages.
The error messages of "fix(data)" :
Error in .External2(C_dataentry, datalist, modes) :
unable to start data editor
In addition: Warning message:
In edit.data.frame(get(subx, envir = parent), title = subx, ...) :
unable to open display
The error message of "edit(data)" :
Error in edit : Editing of data frames and matrixes is not supported in RStudio.
I ran the same codes on my local rstudio (windows 8.1), and all the functions work fine!
It's really wired... Anyone can help me? Thanks a lot!
p.s. I read this post already, but it doesn't work for me.
sessionInfo:
R version 3.1.3 (2015-03-09)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)
locale:
[1] LC_CTYPE=zh_TW.UTF-8 LC_NUMERIC=C LC_TIME=zh_TW.UTF-8
[4] LC_COLLATE=zh_TW.UTF-8 LC_MONETARY=zh_TW.UTF-8 LC_MESSAGES=zh_TW.UTF-8
[7] LC_PAPER=zh_TW.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=zh_TW.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.1.3
I had the same problem, but then I installed the XQuartz package from here: https://www.xquartz.org/
And then another thread, from a while back, reminded me that I needed to restart my Mac for the x11 app to take effect: https://stackoverflow.com/a/36486555

Cannot find 'convert' using SaveGIF function in animation package

I'm having trouble with the SaveGIF function in library (animation). The function gives me this error:
Error in cmd.fun(sprintf("%s --version", convert), intern = TRUE, ignore.stdout = !interactive(), :
unused argument(s) (ignore.stdout = !interactive())
I cannot find ImageMagick with convert = 'convert'
NULL
Warning message:
In im.convert(img.files, output = movie.name, convert = convert, :
Please install ImageMagick first or put its bin path into the system PATH variable
What confuses me is that I do have convert on my system, so I'd expect SaveGIF to be able to find it without any trouble:
me#my-laptop:~$ convert --version
Version: ImageMagick 6.5.7-8 2010-12-02 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2009
How do I add convert's bin path to the system PATH variable?
Edit: I'm using Ubuntu, not Windows.
You probably did not edit your PATH variable. On Windows 7 (but will probably work on earlier versions to) go to:
start menu
Right click computer -> properties
Advanced System Settings
Environment Variables
Then under "System variables" (the lower pane) find the variable "path", select it
and click edit.
You are interested in the content of Variable value (maybe copy it to a text editor to read it better). It should contain the link to imagemagick in it, seperated by semicolons from other variables. For me the path to imagemagick is:
C:\Program Files\ImageMagick-6.6.7-Q16
Make sure you only add it, not change anything else to the path variable. Add it with a semicolon.
Usually we do not need to manually set the PATH variable under Linux. I'm using Ubuntu as well, but I cannot reproduce your error. What's your R version?
> sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] animation_2.0-4
As far as I can see, the error comes from the function system() instead of not being able to find convert. The argument ignore.stdout came in R 2.12.1, so you must be using a lower version of R. The current version is 2.13.0, and I recommend you to upgrade to the latest version. See http://cran.r-project.org/bin/linux/ubuntu/ for how to keep R updated with CRAN.
You can find where your convert function lives with system("which convert", intern=TRUE) and then you (may be able to) add that to your PATH variable with
Sys.setenv(PATH=...)
After attempting all of these fixes as well as these and these to no success, I used alternative software to make the conversion from the png files that were successfully created with saveHTML. Several programs are described here. I am a Windows user and found the simple instructions contained in that site for VirtualDub quickly accomplished this task.

Resources