Execute a .bat file within the R console - r

Is it possible to Execute a .bat file within the R console? I know I can run R in batch mode, but am wondering if I can start a .bat file from the R code within itself. I have googled it and cannot seem to find an answer. Thanks in advance!

I guess you could use system command for invoking OS commands. Please check here for more info.

Related

Problems executing a batch file from R

I am creating a function in R and in one step of the function I run a batch file, this batch file in turn runs a different program which creates files that I then want to read in my function.
I am using shell.exec to run the batch file and it runs fine, the problem is that the next line of my code that wants to read in the output from the program ran by the batch file crashes because it hasn't been created yet.
So I get an error the first time I call my function but if I simply call it again it runs fine. Example of code below: Basically what happens is I get an error message when calling the function saying that .../bat_output.txt does not exist, because the batch file hasn't been run yet, but then when I call the function again, it works fine.
shell.exec("run.bat")
readout<-read.table("bat_output.txt")
Any suggestions?
shell.exec returns immediately while the script is running in the background. The reason bat_output.txt is not found the first time is likely that the script has not finished yet. shell.exec does not give you the ability to wait or any information to determine if the process is still running, so it might not be the best tool for this.
Alternatives:
system("cmd /c run.bat")
system2("cmd", c("/c", "run.bat"))
Realize that if you reference a different path, you might want/need to normalizePath and/or dQuote it going into those commands. (R's system* commands are bad at argument forming.)

Scheduling R Script - OSX

I have written a series of R Scripts that create csv files. From there, Tableau will read the csv's and update various dashboards. As Tableau can easily be scheduled to update on a daily cadence, I was hoping to do the same with my R Script.
While there are a bunch of answers already with solutions for Windows, there hasn't been a solution posted for OSX. I have looked into trying to run my script in Terminal and use automator to do it, but couldn't quite figure it out. Basically, when the shell script runs it terminates midway through because there are errors in the R Script - but I do not care about the errors. The Automator didn't work as well.
Additionally, I also looked into Data Integration/Pentaho but the additional software configuration and subsequent installation seemed difficult.
Any help or insight would be greatly appreciated! Thanks!
Type crontab -e and add this line to the resulting file
#daily Rscript 1.R && Rscript 2.R
It will run the files 1.R, followed by 2.R at midnight every day. Hope that helps.
The most flexible way to do this is to use launchd, the service that manages processes on OS X. You can look at some examples in the official documentation.

Running two instances of Rstudio simultaneously on Linux

I've got a lengthy process running in Rstudio and I would like to open a separate session of Rstudio while the first one is running. I know I can run R from the command line to get as many sessions as I want, but I wanted to know if it is possible for me to do this in Rstudio on a Linux computer. Thanks.
#infominer suggested a good solution, which is to simply type rstudio in the command line. That's what I ended up doing
Another convenient way to deal with this is to start a seperate R-instance in the terminal by typing simply
R
and from there just run the script that has a lengthy process with
source("path-to-your-script/your-script.R")
you can than continue to edit and work with your two scripts in the already opened R-Studio editor window.

Call R scripts in Matlab

Is it possible to call R scripts in a MATLAB program? How can I do that?
You can use R in batch mode. If R is in your path, then you can call from MATLAB:
system('R CMD BATCH infile outfile');
will run the code in infile and place output in the outfile.
EDIT:
You can also give it a try with another approach using a R package rscproxy and R(D)COM Server, described here.
After using R(D)COM and Matlab R-link for a while, I do not recommend it. The COM interface has trouble parsing many commands and it is difficult to debug the code. I recommend using a system command from Matlab as described in the
R Wiki.
system is almost definitely the way to go, as described in other answers. For completeness, you could also use MATLAB's capability to run Java code, and JRI or RCaller to call R from Java. Similarly, you can use MATLAB's capability for running .NET code and R.NET.
Yes. On Windows, I have done a lot of this via the Matlab R-link and then R(D)COM server on the R side.
It works beautifully for passing commands and data back and forth. Calling R via the OS is feasible, but then you have to deparse (write) and parse (load) data passed between them. This is tedious and no fun. Especially if you are much data around. It also means that you lose state on the R side and every invocation is just like the first time.
On Linux or another OS, or even for more general usage, I'd now try Rstudio as a server -- see http://www.rstudio.org/docs/server/getting_started for more info.
Another way RWiki recommended:
CurrentDirectory=strrep(pwd,'\','/');
eval(['!C:\R\R-3.0.1\bin/Rscript "' CurrentDirectory '/Commands.R"'])
You can run command line functions in matlab using the unix command. The easiest way would probably be to set up an R script which outputs results to a text file, run the script in matlab using the unix command, and then (in matlab) verify that the file exists and load it up.
You could use the system command to execute R scripts. Something like the following:
[status] = system('R CMD BATCH [options] script.R [outfile]')
where [options] are the options your send to the R interpreter, and [outfile] is your output file.

Run Batch File from R

I've found a lot of answers on how to run R from a Batch file, but nothing about running a Batch File from R. I know one way to do this is to use system, system2 or shell, but these methods wait for process in the Windows Command Prompt to finish before R accepts another input. I want to run a Batch File which calls a console application that runs indefinitely, and then allow R to do other things. Any help would be greatly appreciated.
The help page ?shell says how to do it. Just run
shell("MyScript.bat", wait=FALSE)

Resources