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

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.

Related

Need help getting bash/batch to work for R on Windows 10

I'm trying to get batch mode working for R on Windows 10. The ultimate goal is to run many iterations of some R code in batch on an external server.
I successfully installed bash (unix wrapper for windows 10?) on my cmd prompt. I am working through a tutorial on using batch. I'm not sure if I want this to run through cmd or through the r code directly? https://github.com/gastonstat/tutorial-R-noninteractive/blob/master/02-batch-mode.Rmd
Via the tutorial I am working on testing batch/bash with simple code myscript1.R. Then the code I enter into cmd promp/bash looks like:
R CMD BATCH "F:/Google Drive/Documents/batch/myscript1.R" "F:/Google Drive/Documents/batch/myscript1-output.R"
Currently the closest I get in the cmd/bash is that an output file is created in the right folder but blank and I am told \usr\lib\R\bin\BATCH: cannot create myscript1-output.R: Permission denied.
I have done everything possible to allow full permissions to all users and not sure what is going on. Can anyone who knows how to use batch mode or bash in R for windows advised me?
Thank you
Answer here thanks to Phil. I did not need to use "Bash" through Ubunto... I think.
Instead, I just needed to call the CMD BATCH through regular cmd with three parts:
the directory of my R.exe: "C:\Program Files\R\R-3.5.3\bin\x64\R.exe" (replace version)
CMD BATCH
directory of the project file/script: "F:\project_folders_batch\myscript1.R"
directory of the desired output (so it doesn't default to C/users/username). In this case, I output to the same folder as the script: "F:\project_folders_batch\myscript1-output.R"
Also, in case you are outputting plots or anything (I was), go ahead and cd (change directory) to the project folder before you do this. Final result in 2 steps:
cd /d "F:\projectfolder\batch"
"C:\Program Files\R\R-3.5.3\bin\x64\R.exe" CMD BATCH "F:\projectfolder\batch\myscript1.R" "F:\projectfolder\batch\myscript1-output.R"
Also mind your antivirus... it blocked access a few times.

R script to Task Scheduler

I have an R script that gets data from databases on another server and brings it into my database. I have it saved as "dataimport.R"
I followed a few answers from here and from other websites and created a batch file like this:
"C:\Program Files\R\R-3.4.0\bin\R.exe" CMD BATCH --vanilla --slave "C:\dataimport.R"
This is not working. The cmd window opens up but the tables are not recreated and I dont get any error. I wanted to run the Task Scheduler to automate the process. Any ideas on how to fix this?
I kept at it and interestingly the answer to this was this:
"C:\Program Files\R\R-3.4.0\bin\R.exe" "C:\dataimport.R"
I dont know the reason for this but as long as it works.
I've had loads of problems with this, but finally managed to get it to work. To be more comprehensive, here are some of the things I've tried (in case one of these work for other persons):
#echo off, R CMD BATCH C:\myfolder\script.R
R CMD BATCH C:\myfolder\script.R
Using the package taskschedulerR (somehow didn't work overnight)
Used the answer provided above ("C:\Program Files\R\R-3.4.0\bin\R.exe" "C:\dataimport.R")
and all kinds of variations and combinations off these. (can't remember them all exactly)
What finally worked was:
Make an R script and save it (C:\myfolder\Test.R for example)
Through notepad, fill in: "C:\Program Files\R\R-3.5.2\bin\x64\R.exe" CMD BATCH "C:\myfolder\Test.R" (also tried Rscript.exe, didn't work for me).
in Windows Task Scheduler (v1.0) do 'Create task'
fill in time triggers.
in Actions, make an action with Start a program and in the Program/Script line provide the location where your bat script is. C:\myfolder\Test.bat
in the Start in (optional) line: enter C:\myfolder\
Note: both your .bat file and .R script are in the "C:\myfolder" folder.

Executing a batch file in an R script

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)

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.

Problems scheduling an R Script and saving data to the wd

I've just set up an R script to run on my Windows machine - I'm trying to have it save a dataframe on the working directory (which I know from getwd()).
I can see from the Task Scheduler that the script must be running as saves the last run time, however when I check the wd for new time stamps on the dataframes I'm trying to save, they haven't updated? (I save over them each time, or at least that's what I'd like to do, I manually saved them in there to start with).
I'm using this on the scheduler:
C:\Program Files\R\R-2.13.1\bin\R.exe" CMD BATCH  --vanilla --slave “C:\my projects\my_script.R
That appears to be working, but can anyone offer a reason as to why the script that I call doesn't seem to be saving my NEW DF to the wd? I'm using this command to save the DF:
write.table(m23,file="m23.csv",sep=",",row.names=F)
so DF m23 should get updated everyday in the wd when the scheduler calls the script at 6am?
Paul.
Are you sure you know what the current working directory is when the scheduler runs the script? My guess is it might not be what you think is it. I would look at the answers to this question: Rscript: Determine path of the executing script, especially the suggestion about using commandArgs to figure out where you are. Or you can explicitly set the working directory in your script with setwd()

Resources