How to use VBA to run R script - r

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.

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.

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

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?

Execute R script in R studio using batch file

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"

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.

Run R script using batch file

I'm attempting to schedule an R script as mentioned in
the Scheduling R Script stackoverflow answer but I keep getting an error that says
ARGUMENT 'BATCH' \__ignored__
ARGUEMENT 'C:/Users/cburton/AutomaticJobPuller/jobsPuller.R' \__ignored__
and then it has the startup prompt for the R programming environment.
The contents in my batch file are
C:\Users\cburton\Documents\R\R-3.2.0\bin\R.exe BATCH C:\Users\cburton\AutomaticJobPuller\jobsPuller.R --vanilla
This is my first batch file, so I'm sure it's something simple, but I can't find the answer to this.

Resources