Run .R functions via CMD - missing execute all command? - r

Good day,
I want to automate data cleaning and merging via R. I have created multiple folders to which you upload excel files. Than with R I clean the data and merge them to a final file.
I would like to do this process without running R or Rstudio. To accomplish this I run R via CMD (.bat), however, the code does not run.
In my R script I have multiple functions calling from other files, I assume that I need a simple "Run all command" to execute the code via CMD?
Example of Combine_all_excel_files.R
setwd("C:\\Code source\\")
source("Seperate_input_folder_by_years.R")
Seperate_input_folder_by_years()
Merge data.bat file:
start "" "C:\Program Files\R\R-3.3.3\bin\x64\Rscript.exe" CMD BATCH "C:\Code source\Combine_all_excel_files.R"
pause
When I run the .bad file, the code does not run. What I am doing wrong?

Related

Running several R scripts at once using .bat file with CMD BATCH?

I wanted to use a Windows .bat file to run several R scripts and schedule it using windows scheduler.
However, when I test using windows scheduler, everything executes but the files that the R scripts should create are never created. Instead, when I double-click on the .bat file itself it works fine. My goal is for several scripts to just run overnight without me going in and running each one of them manually one-by-one.
If I were to add a second line to the .bat file that would make it run a second script, would this execute only after the first is complete? If not, would I be able to delay the second until the first finishes somehow?? For instance, my .bat file looks like this:
"C:\Program Files\R\R-3.4.2\bin\x64\R.exe" CMD BATCH C:\Users\gma\Desktop\R_Task\script1.R
"C:\Program Files\R\R-3.4.2\bin\x64\R.exe" CMD BATCH C:\Users\gma\Desktop\R_Task\script2.R
I used the answer provided by #Gautam (R taskscheduleR not executing script) to get this far

Can I prevent R from creating .RData files?

I am trying to share some R code with colleagues who don't know R. I have created a batch file so they can just double-click it and run the R script without even opening R. But it creates a .RData file.
My question is, can I prevent R from creating the .RData file?
I've read here Disable saving history that I could disable it through RStudio global options but my colleagues are installing just R and won't need to ever open it, so I am looking for some kind of solution of the likes of options(...) that I can just put in my Rscript, or maybe something that could be speficied in the batch file call.
For anyone curious, I figured it out thanks to #r2evans and this post: https://es.stackoverflow.com/questions/166211/evitar-que-r-cree-ficheros-r-data-y-r-history.
I just had to add --no-save to my batch call like this:
R CMD BATCH --no-save "%file%" NUL
where %file% is the Rscript path. The NUL part is so a .Rout file won't be created after running the batch file.
Also, doing
Rscript.exe "%file%"
runs the Rscript and doesn't produce .RData or .Rout files. The difference is that R messages get printed on the command window as they would in an interactive R session.

How to use VBA to run R script

I'm having trouble running my R script from Excel VBA.
I wanted to create a Button in Excel with a macro that runs an R script but I've tried so many versions and none worked.
My path to R is "C:\Program Files\R\R-4.1.1\bin\Rscript.exe"
The path to my R Script is "\repo\xyz\18320\38293\one\redirected folder\Desktop\test_run\test\Code\sample.R"
How can I create a shell on VBA which actually executes the Script?
Can I maybe execute the script from the command line and then connect the excel macro to the command line? I didnĀ“t manage to find a solution.

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.

Using R and with the Terminal

I have combed the internet looking for a tutorial on how to use R through the terminal and I can't find anything that exactly fits what I'm looking for. I have a group of R scripts. They need to be run in a specific order. Each produces a set of .csv files to be used as inputs for the next script that will be executed.
I have used R CMD BATCH filename.R and it makes an output file called filename.Rout
But then when I try to use cat filename.Rout, I get an error Fatal error: cannot open file 'filename.R': No such file or directory
Also I'm not sure where my output (.csv files) should be written to. In my R script, there are some write.csv lines pointing the new datasets to be written to a specific directory but when I run R CMD BATCH filename.R in the Terminal, I don't see the files where they should be. I see a filename.Rout in another directory but I can't open it.

Resources