Executing a batch file in an R script - r

I would like to execute a batch file from a R script. The file is in a directory like \\network\path\to\batch\file.bat.
I know I can use the system command in R to run DOS commands but I can't simply use system("start file.bat"). So how would I best use R script to execute this batch file?

Try shell.exec("\\\\network\\path\\file.bat")
The shell.exec command uses the Windows-associated application to open the file. Note the double back-ticks.
Pro tip: write.csv(file='tmp.csv',tmpdat);shell.exec('tmp.csv') is useful (assuming you've associated CSV files with your preferred application for viewing CSV files) for quickly checking output.

try
shell('\network\path\to\batch\file.bat')

I found this problem when using RSelenium in Windows as well but using this batch file made sure to close all chromedriver processes. I was ending up with a ton of these processes after a lengthy scraping session.
My solution was to execute the batch file from within the R script every so often by using:
shell.exec(file.path(getwd(), "kill_chromedriver.bat"))

This is what I did on windows platform.
cmd='\\prj\\whatkit\\test.bat'
system(cmd, intern = TRUE, show.output.on.console = TRUE)

Related

Trying to create an executable to auto-run an R-script

I Googled this a bit, and found a few dead-ends, but I can't seem to create an executable file that runs an R-script. I am working on Windows 7. I am looking for a solution that I can double-click, or run on Windows Task Scheduler. Even if I have to run it using a batch script, that's totally fine. I am just looking for a way to run an R-script based on a date/time condition.
For instance, if I run this in R, saves some data, in Excel format, on my desktop.
target = "http://www.abs.gov.au/ausstats/meisubs.NSF/log?openagent&5206001_key_aggregates.xls&5206.0&Time%20Series%20Spreadsheet&24FF946FB10A10CDCA258192001DAC4B&0&Jun%202017&06.09.2017&Latest"
dest = 'C:\\Users\\Excel\\Desktop\\downloaded_file.xls'
download.file(url = target, destfile = dest, mode='wb')
Now, I save that as 'Sheet_Names.r' on my desktop and also save a batch file, with this inside: '"C:\Program Files\RStudio\bin\rstudio.exe" CMD BATCH C:\Users\Excel\Desktop\Sheet_Names.r' without the quotes. Then, I run the batch file, as administrator...but nothing happens...
All suggestions are welcome.
Thanks.

How to use PowerShell to schedule sourcing an R script?

My goal is to use PowerShell to schedule the sourcing of an R script.
My current work flow is that I open RStudio, click the "Source" button in the upper right corner. Then I wait until it's finished, and close RStudio. I change nothing in the R script.
In PowerShell I've been using its Register-ScheduledJob cmdlet to kick off C# programs on a daily schedule. And here's the problem, I can't find an example of effectively using PowerShell to source an R script.
I believe the PowerShell script should probably use the Invoke-Expression cmdlet. But I'm not 100% sure.
To no avail I've tried this:
Start-Process "C:\Program Files\R\R-3.2.4revised\bin\x64\Rterm.exe" -RedirectStandardInput "C:\MyScript.R"
Also, I'd like to avoid the solution that uses CMD BATCH as that's defeating the purpose of using PowerShell.
If just sourcing the R script is what you're looking for then one way to do is something like this
& "C:\Program Files\R\R-3.1.1\bin\Rscript.exe" "C:/Program Files/R/R-3.1.1/tests/demos.R"
where "C:\Program Files\R\R-3.1.1\bin\Rscript.exe" is path Rscript in your local R installation and "C:/Program Files/R/R-3.1.1/tests/demos.R" is path to script you'd normally source() directly in RStudio.
One thing to keep in mind is depending on location of files your R script needs you might need to adjust your script with appropriate setwd()

Run R web scrape daily

I have a script written in R that pulls online and then exports it to a spreadsheet on my computer. I am attempting to create a batch file so that I can set up a scheduled task, but it only opens up the R file without actually running it. The batch file is as follows:
"C:\Program Files\R\R-3.0.2\bin\Rscript.exe" CMD BATCH
"C:\Users\xxx\OneDrive\xxx\Scraper.r"
I am very open to ideas other than creating a batch file, I just want this to work!
Try using this instead (note the x64 addition)
"C:\Program Files\R\R-3.0.2\bin\x64\Rscript.exe" C:\Users\xxx\OneDrive\xxx\Scraper.r
and without the CMD BATCH flags. That works for me.
I am not sure what is going on, but it may be you only have the 64 bit version installed, and you are trying to execute the 32 bit version. But I get a silent fail when I don't have specify the "x64" sub-directory.

batch process for R gui

I have created a batch file to launch R scripts in Rterm.exe. This works well for regular weekly tasks. The < PBWeeklyMeetingScriptV3.R > is the R script run by Rterm.
set R_TERM="C:\Program Files\R\R-2.14.0\bin\x64\Rterm.exe"
%R_TERM% --slave --no-restore --no-save --args 20120401 20110403 01-apr-12 03-apr-11 < PBWeeklyMeetingScriptV3.R > PBWeeklyMeetingScriptV3.batch 2> error.txt
I've tried to modify this to launch the R GUI instead of the background process as I'd like to inspect and potentially manipulate and inspect the data.
If I change my batch file to:
set R_TERM="C:\Program Files\R\R-2.14.0\bin\x64\Rgui.exe"
the batch file will launch the R GUI but doesn't start the script. Is there a way to launch the script too?
Alternatively is there a way to save/load the work space image to access the variables that are created in the script?
You can save and load workspaces by using save.image() and load(). I do this all the time when scripting to pass data sets between two separate script files, tied together using Python or bash. At the end of each R script, just add:
save.image("Your_image_name.RData")
The image will be the workspace that existed whenever the command was run (so, if it's the last command in the file, it's the workspace right before the exist of the file). We also use this at my job to create "snapshots" of input and output data, so we can reproduce the research later. (We use a simple naming convention to get the time of run, and then label the files with that).
Not sure about launching and then running the GUI with specific scripts in it; I don't think that's a feature you'll find in R, simply because the whole point of running a batch file is usually to avoid the GUI. But hopefully, you can just save the image to disk, and then look at it or pass it to other programs as needed. Hope that helps!

How to run a R language(.r) file using Batch file?

I want to run a R script file (.r) using batch file.
If R.exe is in your PATH, then your windows batch file (.bat) would simply consist of one line:
R CMD BATCH your_r_script.R
otherwise, you need to give the path of R.exe, so for example:
"C:\Program Files\R\R-2.13.0\bin\R.exe" CMD BATCH your_r_script.R
you can add certain input arguments to the BATCH command, such as --no-save, --no-restore
so it would be
R CMD BATCH [options] your_r_script.R
more info on options, etc at http://stat.ethz.ch/R-manual/R-devel/library/utils/html/BATCH.html
Note: R uses the command "BATCH" to non-interactively evaluate a script located in a file. Here we are running the command "BATCH" from a windows .BAT file, but that's merely a coincidence.
An answer to another question suggests using Rscript.exe, so your batch file would contain:
"C:\Program Files\R\R-3.0.2\bin\i386\Rscript.exe" your_script.R
pause
It is a good idea to add R to the windows environment path. In a comment in this question #chase gave a link that explains how to set the path on windows 7. Once R is added to the windows path, your batch file should become simply :
Rscript.exe your_script.R
pause
You can also directly call a R command by using the -e flag. For example this batchfile will tell R to set its current working directory to Documents, then it will print the working directory:
Rscript.exe -e setwd('Documents');getwd()
pause
I struggled with the syntax with the answers below, but this worked for me in the .bat file:
C:\Windows\System32\cmd.exe /k ""path to Rscript.exe"
"path to .R script""
Be sure to place both the path to Rscript.exe and the script in "" together and separately as above.
I doubt you will be able to run it using a batch file.
http://www.fileinfo.com/extension/r
Most known programs that use .r files do so for source code files it looks like. You will probably have to compile it using the program it was written for. I guess you could use a command line compiler from a batch file, but I don't know what language or applications you are using.
If you post the script file or give more information about it, we could probably help you better.

Resources