pandoc from RSudio Server not recognized when running script via cron - r

On a server, I want cron to run an R script which renders an HTML page using rmarkdown. RStudio server is installed. The crontab entry for user mark is:
* * * * * Rscript R/test.R >> /tmp/cron.log 2>&1
test.R:
library(rmarkdown)
getwd()
render("R/test.Rmd")
The cron.log file shows
[1] "/home/mark"
Error: pandoc version 1.12.3 or higher is required and was not found.
Execution stopped
Running test.R from the console, however, works fine:
Rscript R/test.R
The RStudio server version of pandoc was added to usr/local/bin using a symlink as described here). Checking the pandoc version in the console gives
mark#myserver:$ pandoc -v
pandoc 1.15.2
which is not the old version as in the log file. Also, the binary appears to be found
mark#myserver:$ which pandoc
/usr/local/bin/pandoc
I am not sure what is going on. Any ideas?

It's likely that /usr/local/bin is being added to your $PATH in your bash shell, but that your cron job is not running under bash and so doesn't have pandoc on the path. To test this assumption, add these lines to your R file:
Sys.getenv("PATH")
Sys.which("pandoc")
If that's indeed the case, you could have your R script append to $PATH as appropriate, or have cron run the job under a bash shell.
If all else fails, you could set RSTUDIO_PANDOC:
Sys.setenv(RSTUDIO_PANDOC = "/usr/local/bin/pandoc")

Related

texi2dvi() error when running a .Rnw script with LaunchControl

I'm trying to compile a knitr script on a timer using LaunchControl (a launchd GUI for scheduling cron-like jobs on OSX).
I have a dispatcher.R script that does this:
#!/Library/Frameworks/R.framework/Resources/Rscript
library("knitr")
setwd("~/somedirectory")
knit2pdf("my_script.Rnw", output= "my_script.tex")
When I run it interactively from in RStudio, my_script.Rnw works great. I get the desired PDF output. However, when launchd runs the dispatcher.R script I get this error:
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'my_script.tex' failed.
Execution halted
The .tex file gets generated, but then it doesn't compile. I a would say it was problem with my LaTeX installation path, but since it works using knit2pdf() I'm not sure. What could be the issue?
Still working on this. Updates:
No .log file gets produced with knit2pdf() via LaunchControl, but I get a .tex file and /figure folder.
I updated MacTex and also tried a minimal example of an empty document and I got the same error about texi2dvi.
When I run knit2pdf("my_script.Rnw", output = "my_script.tex") using LaunchControl and then go back to RStudio and run texi2dvi("my_script.tex", pdf = TRUE), then I get the desired outcome.
The problem reproduces on Sierra and Yosemite
On Sierra there is an additional error about In my_script_latex_pkg("framed", system.file("misc", "framed.sty", package = "knitr")) : unable to find LaTeX package 'framed'; will use a copy from knitr
I tried Sys.setenv(PATH = paste(Sys.getenv("PATH"),"/usr/texbin",sep=":")) and it didn't help.
Running $ Rscript dispatcher.R from the command line works just fine. The PDF compiles.
Running a bash script with Rscript dispatcher.Rin LaunchControl does not work; same error about texi2dvi.
To run a .Rnw file using LaunchControl for task scheduling, create the following files in the same directory. Then, run the *.sh script in the scheduler. Voila! The problem I was encountering in my original post was the LaunchControl doesn't (by default, at least) read ~/.bash_profile, so adding the PATH variable into the .sh script resolves this.
1) Your *.Rnw script
This is any knitr script that you can compile without issue from RStudio.
2) A *.R script
#!/Library/Frameworks/R.framework/Resources/Rscript
library("knitr")
setwd("~/some_directory")
knit2pdf("yourscript.Rnw", output = "yourscript.tex")
3) A *.sh script
Make sure that you have the PATH variable to your LaTeX installation.
#! /bin/bash
PATH="/usr/texbin:${PATH}"
export PATH
Rscript yourscript_dispatcher.R
This solution works on OSX Yosemite 10.10.5 on R version 3.3.2 (2016-10-31).

Pandoc while using R markdown with a cron command

I'm trying to create a cron command that will use R markdown to create a new html page at specified intervals. I've discovered this is a pandoc issue.
I get the following error message when I log my cron command
Error: pandoc version 1.12.3 or higher is required and was not found
(see the help page ?rmarkdown::pandoc_available). Execution halted
Is there a simple bit of code I can add to the .Rmd file to point it to pandoc when executing the cron command?
Preserving the original post. That is below this paragraph.
Everything I want to do is a a file titled test_doc.Rmd.
When I run the following command on the command line, it works successfully:
RScript -e "library(rmarkdown); render(\"/path/test_doc.Rmd\")"
However, when I run that in the crontab, I'm having no success. I'm running a version of this:
25 10 * * * RScript -e "library(rmarkdown); render(\"/path/test_doc.Rmd\")"
I'm baffled. I don't believe it's a filepath issue, since I have other R scripts (not rmarkdown) running in the crontab and working. I am on Mac OS X 10.10.5
The same has happened to me, and I found the answer in a related post regarding your error message (which I haven't even seen):
Error: pandoc version 1.12.3 or higher is required and was not found (see the help page ?rmarkdown::pandoc_available). Execution halted
You have to specify the RSTUDIO_PANDOC environment variable before rendering like so:
Rscript -e 'Sys.setenv(RSTUDIO_PANDOC="/usr/lib/rstudio/bin/pandoc"); rmarkdown::render("test_doc.Rmd")'
This should solve your cronjob issue. It worked for me.
I am assuming that most Linux+RStudio users have pandoc installed in this /usr/... path. Otherwise, query the location using Sys.getenv("RSTUDIO_PANDOC") from an interactive session where the knitting works, and substitute the path in the above command.
Try
25 10 * * * cd /path && Rscript -e 'rmarkdown::render("test_doc.Rmd")'
which avoids
The full path and gives rmarkdown and knitr a better working directory
The need to 'quote quotes' by having apostrophes on the outside and standard double quotes on the inside.
Add the path to the beginning of your cron, and redirect the output for debugging purposes:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/5 * * * * cd /path/to/script/ && Rscript -e 'library(rmarkdown); rmarkdown::render("your_script.Rmd")' >/path/to/script/cron.log 2>/path/to/script/cronerr.log

Rscript: command not found

I'm working with R for a while, and I always worked with Rstudio, I tried just now to run a Rscript command in terminal (I have a mac..) and I got this error-
>Rscript script.R
-bash: Rscript: command not found
when I tried to open R in the terminal I go the same error-
>R
-bash: R: command not found
I can run R code with the Rstudio and the R application, but I know there is a way to run R throw the terminal.
Did I miss something when I installed R on my computer? do I need to add R to my PATH?
thanks in advance!
Steps to run R script through Windows command prompt
Set the PATH variable for Rscript.exein the environment variables. Rscript.exe can be found inside bin folder of R. Set the path for Rscript.exe to use Rscript command in Windows command prompt. To check if Rscript.exe has been set environmentally or not, type Rscript in command prompt. The follwoing message should come.
Go to Command Prompt, set the path where your .R file is there.
Run the following command: Here abcd.R is present under Documents folder. So I set path and then run Rscript abcd.R
For those who stumbled upon this but use a mac, you might find this useful. I recently downloaded and installed R and RStudio through the CRAN site. I didn't do it through homebrew. Since I downloaded this install directly from the site, it DID NOT add the RScript executable to my /usr/local/bin directory.
I have locate on my mac so I did a quick lookup:
locate RScript
And I found it here:
/Library/Frameworks/R.framework/Versions/4.0/Resources/bin/Rscript
What I had to do was create a symbolic link to my /usr/local/bin directory to get it to work:
cd /usr/local/bin
ln -s /Library/Frameworks/R.framework/Versions/4.0/Resources/bin/Rscript Rscript
Now I'm able to run Rscript through the command line. This may help someone else out there.

Permissions-error using bash-script to install a library in R

I am running a software called CNV-Seq in a Bash script but, I do not have permission to run the following command:
R CMD INSTALL cnv/
The error is
* installing to library '/share/apps/r/3.2.2/intel/lib64/R/library'
Error: ERROR: no permission to install to directory '/share/apps/r/3.2.2/intel/lib64/R/library'
How can I solve this problem without gaining permission.
One approach is creating the bash script that executes R script.
So, the R script will contain:
install.packages("package_name")
Save it as xyz.r
and the bash script will contain:
R -f path_to/xyz.r
Save that as abc.
Run the bash script like this:
bash abc

/usr/bin/env: RScript: No such file or directory | After recent R-3.0.1. installation.

I am a bit lost when dealing with installing and using R. I installed R 3.0.1 from source and did the ./configure, make, make check, and make install as suggested. However I tried running R but it said that R wasn't in the /usr/bin folder. So I then copied the entire R-3.0.1/bin directory into my /usr/bin directory using cp. Now I'm getting a few errors regarding /usr/bin/env when trying to use RScript on a hello_world.R script I wrote from the O'Reilly R In a Nutshell book I store in a file hello_world.R the contents are below:
#! /usr/bin/env RScript
print("Hello World!");
Simple enough, but when I try to load it I get the following error:
$ ./hello_world.R
/usr/bin/env: RScript: No such file or directory
I'm not sure if this is a PATH problem or something, but when I search in my /usr/bin directory I do see the RScript file in there along with (R, BATCH, and the others associated with R programming language). Any help is greatly appreciated. Cheers.
You may be using an invalid command line option for Rscript in your shebang line.
For instance ...
#!/usr/bin/env RScript --vanilla
remove "--vanilla" (or other offending option) and rerun your script
#!/usr/bin/env RScript
I know you didn't put this in your example, but the solution may help others searching for the same issue.
Again, the good solution to this problem is very simple and clearly explained in the man page of env. The script should use the env command to invoke Rscript and not Rscript directly:
#!/usr/bin/env Rscript
some R code now...
But a script like this will read the user's .Rprofile among other things. When we want to have a vanilla R session (in order to start with a clean and controlled R), we must pass the option --vanilla. If you try something like
#!/usr/bin/env Rscript --vanilla
some R code now...
env will take the string Rscript --vanilla a the command to execute and will inevitably return the error message
/usr/bin/env: ‘Rscript --vanilla’: No such file or directory
In env's man page, there is an option called -S for splitting the strings. Its role is exactly to solve the problem above and use the first string Rscript as the command name, and the following strings (like --vanilla) as options to pass to Rscript.
The solution is therefore:
#!/usr/bin/env -S Rscript --vanilla
some R code now...
Put in the shebang line of your script #!/usr/bin/Rscript and it should work.
As a side remark if you want to keep up-to-date with the R versions from CRAN and not relying on the native R of your Linux distro (Ubuntu) then add the following line in your apt sources:
deb http://my_favorite_cran_mirror/bin/linux/ubuntu raring/
After that you can always use the apt system to install R which -I would agree with Jake above- it should be the preferable way to install R.
*Change the my_favorite_cran_mirror with a valid CRAN mirror that is close to you.
#! /usr/bin/env RScript
print("Hello World!");
Simple enough, but when I try to load it I get the following error:
$ ./hello_world.R
/usr/bin/env: RScript: No such file or directory
Here u make mistake is that instead of RScript write Rscript.
The syntax will be
#! /usr/bin/env Rscript
print("Hello World!");
Then run it it will work (y) all the best.
$./hello_world.R
I arrived at this question trying to understand this error message on a cluster computer where I did not have control over the R installation.
In general, when I converted Rscript in my makefile to /usr/bin/Rscript the error message no longer occurred.

Resources