Pandoc while using R markdown with a cron command - r

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

Related

crontab: tmp/tmp.X: Operation not permitted in RStudio using `cronR` on Mac OSX Mojave

I want to schedule an R script to run regularly. The most obvious attempt I found when googling is using the cronR package and the RStudio Add-In that comes with it.
After creating a dummy script containing only cat("Hello World") I opened the Add-In and created the job. When trying to start the job I ended up with the error message
crontab: tmp/tmp.X: Operation not permitted
This led me to the question here. I granted Terminal and cron full Disk access. The problem remained.
Edit after comment by #Maylo:
I tried adding the job manually using crontab -e. The line I added was:
0 * * * * /usr/bin/Rscript /Users/me/Documents/R/test.R
After quitting crontab -e with :wq I got the following message on my terminal:
crontab: no crontab for me - using an empty one
crontab: installing new crontab
crontab: tmp/tmp.37904: Operation not permitted
crontab: edits left in /tmp/crontab.Jw9DPv2aBd
What else may cause this error message / problem?
Are there any other solutions to run a script regularly? (say every hour)
Thanks in advance for any help!

CRON not executing R script file

I am trying to automate running of a script (test.R) in R stored on a desktop folder (cron_test). I am using CRON in my mac terminal. The example R script generates random numbers, plots them, and saves them to a PDF:
setwd("~/Desktop/cron_test")
pdf("example_plot.pdf", width = 8, height = 8)
plot(rnorm(100))
dev.off()
My CRON code attempts to call this every minute, but nothing appears in the folder:
* * * * * Rscript /Users/username/Desktop/cron_test/test.R
Nothing is being generated. I have tried the following:
The file runs fine if I call it from the terminal, suggesting there is not an issue with the code:
Rscript /Users/username/Desktop/cron_test/test.R
I have also ensured (and double-checked) that the permissions iof the file has executable permission, as per this proposed answer to a similar issue:
chmod 777 /Users/username/Desktop/cron_test/test.R
I have also tried the other proposed solution from this answer, which is to create a shell file that calls the R script. This doesn't work, either.
I am receiving mail to state that the CRON task has run, but no output is appearing in my cron_test folder.
Does the mail say
/bin/sh: Rscript: command not found
?
If so, try using the full path to Rscript. You can find this from the terminal with
$ which Rscript
The reason for this is that the environment you get in a cron session is much smaller compared to that in an interactive terminal. You can examine this by comparing the outputs of
$ /usr/bin/env > env-interactive
and the cron job
* * * * * /usr/bin/env > env-cron
We can see these are quite different by counting the number of lines in each file.
$ wc -l env-{interactive,cron}
20 env-interactive
8 env-cron
In particular the shell executable search path ($PATH) in the cron job is much shorter
$ grep PATH env-{interactive,cron}
env-interactive:PATH=/Users/hpcchris/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/opt/X11/bin:/Applications/Wireshark.app/Contents/MacOS:/Users/hpcchris/bin
env-cron:PATH=/usr/bin:/bin
(Using the full path /usr/local/bin/Rscript worked for me with R on my macos High Sierra laptop and produced the plot.)

pandoc from RSudio Server not recognized when running script via cron

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")

R script as cronjob not executing

I've created a short R script that continuously downloads data from twitter using the streamR package. This script is supposed to run on a standard Amazon EC2 server running Ubuntu 14.04. When testing it in the standard command line, it runs fine. However, it is not run as specified in the cronjob. I used the following command:
sudo crontab -e
and then added the following line to the file
0 * * * * Rscript /home/mydirectory/docs/phd-research/data-collection/cron-script.R
in the hope that it would execute the R script every hour.
Is there anything I might have got wrong with the permissions? I've already checked that cron is running and I chmodded the directory the r script is supposed to write to to 775.
Thanks in advance!
OK - it turns out I used the wrong command to call up the crontab. The directory it was supposed to be working in was owned by user rather than root, so calling a root crontab didn't work.
The proper command for editing the crontab is the following:
crontab -u user -e
The Rscript didn't require the full path, it worked with both.
The $PATH$ is not known (i.e. the directory of Rscript is not part of the standard $PATH$) during the execution of a cronjob. Writing the full path to Rscript will work:
0 * * * * /.../Rscript /home/mydirectory/docs/phd-research/data-collection/cron-script.R

/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