Execute R script in R studio using batch file - r

I want to execute a R script in R Studio using batch file. I know how to execute R script using batch file in R though. When I try to execute using the following:
"C:\Program Files\RStudio\bin\rstudio.exe" CMD BATCH --vanilla --slave "C:\Users\kpappala\Desktop\R schedule\task.R"
It just opens R studio but doesn't execute. Is there a way?
Thanks!

Rstudio is an IDE for R. It isn't R though. It doesn't really even make sense to run it in batch through rstudio.
If you're just saying you want to run a file from within Rstudio that's different and you can just source it or run it in batch via system using a call to R CMD BATCH.

This question was answered here:
batch execute R script.
In short and as Dason said, you Rstudio is just a shell, and doesn't actually run the code. For that use R. Try this instead:
PATH C:\Program Files\R\R-3.1.0\bin;%path%
Rscript "C:\Users\kpappala\Desktop\R schedule\task.R"

Related

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.

Running R script using command line on Windows

I'm trying for my first time to run an R script from command line on WINDOWS to automate calculation with another program. So I made my R script using R studio and saved it a .bat file with this line
C:\Program Files\R\R-4.0.2\bin CMD BATCH C:\my_directory\my_script.R
Then I putted the script and files the script have to be executed on in the same directory, the one that contains files to run also the program I need. I came throught command line in that directory and I executed the batch file but it doesn't work. I have this as a error message:
"C:\Program Files\R\R-4.0.2\bin"it is not recognized as an internal or external command,
an executable program or batch file.
Where I am wrong? thank you!
You should use Rscript instead, assuming its in your PATH.
E.g.
Rscript C:\my_directory\my_script.R
Or a single expression
Rscript -e "print(123)"

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()

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