Automate R scripts in Windows
Automating R scripts in Windows is a straightforward process but the process can become a little more complex when a specific R environment (renv) is required and/or if the output is a Quarto document.
In general the process is to create a driver program in a .bat file that launches an renv, runs a script, and produce an output log file to monitor any errors. The .bat file can then be scheduled in Windows Task Scheduler to run on an automatic basis.
There is an additional layer of complexity if the R script renders a Quarto or Markdown report that is meant to be placed in an Egnyte directory. For reasons that are unclear to me, R cannot write to an Egnyte drive when the Task in Windows Task Scheduler is set to run whether or not a user is logged on. The user must be logged on to write to an Egnyte drive. One alternative approach is to render a Quarto report and then upload it to REDCap.
Create an R script that will need to be run on a regular basis
This can be any script. These are frequently .qmd documents for me. For .qmd documents an added layer is neede in that you need the .qmd document, AND an R script that renders the .qmd document.
Create a .bat file that will serve as the driver program
#| eval: false
:: directory to the root project folder in windows backslash format
set proj_root="C:\Users\rodrica2\OneDrive - The University of Colorado Denver\Documents\DFM\projects\hsq"
:: path to the Rscript.exe version used to develop the R script in windows backslash format
set r_version="C:\Program Files\R\R-4.2.2\bin\Rscript.exe"
:: two R commands separated by a semi colon encased in double quotes
:: load renv, then source script. All paths are forward slash and encased in single quotes
set r_commands="renv::load('C:/Users/rodrica2/OneDrive - The University of Colorado Denver/Documents/DFM/projects/hsq'); source('C:/Users/rodrica2/OneDrive - The University of Colorado Denver/Documents/DFM/projects/hsq/task_scheduling_test.R')"
:: path to the log output file in windows back slash format
set log_out="C:\Users\rodrica2\OneDrive - The University of Colorado Denver\Documents\DFM\projects\hsq\.log"
:: Change directory to the projec root
cd %proj_root%
:: Call the script
call %r_version% -e %r_commands% > %log_out% 2>&1
:: Exit the shell
exit
Configure Task Scheduler
- Open Task Scheduler
- Right click on the “Task Scheduler Library” icon in the left-hand pane and select “New Folder” to keep all automated tasks related to a project in one place.
- Select the newly created folder and in the top center pane, right click and select “Create Basic Task”
- Name the new task and give it a description and click Next
- Select the frequency of the task and click Next
- Select the days and time to launch the task and click Next
- Select Start a program and click Next
- Browse to the .bat file that will serve as the driver program
- Finish
To test the task, right click it in the center pane and select Run. One tell is to inspect the .log file to see if there were any errors to ensure it runs smoothly.