Skip to content

Auto execute R script

3 messages · Jason Gasper, Cedrick Johnson

#
Greetings,
I am hoping someone can help me with a automation task.  I would like to 
auto-run a script at a scheduled time each night. Basically, this 
auto-run would would call sqlQuery() and save the results of some math 
to a table- basically archiving a live database table.  Has anyone done 
this before using R? Perhaps the way to approach this is through the 
editor, but I don't see anything in Tinn that would provide this 
capability.  Further the editor would need to be opened (which I guess 
could be done through windows task scheduler) and the R code would then 
need to be ran. 

It seems clunky, but some of the statistics cannot easily be done in our 
database environment.

Thanks-Jason
#
This is on windows I am assuming? On linux, you can write a script, then 
have cron execute it, similar to below, only I think you need to replace 
Rterm with R CMD or something.

Put this in a batch file, and create a scheduled task. Make sure to put 
the "start in" or "working directory" to the same as where your R script is.

This requires that you have %R_HOME% added to your path (on Windows at 
least)

Rterm --verbose --no-restore --file=MorningStartup.r > 
morningsummary-log.txt

Replace MorningStartup.r and morningsummary-log.txt with your R script 
and a log file name of your choice.

HTH
cedrick
Jason Gasper wrote:
#
Thanks Cedrick for your help- the batch file approach makes complete 
sense. It is a windows operating system.
Cedrick W. Johnson wrote: