Executing R Script Programmatically

Executing R script programmatically

To do this in C# you'll need to use

shell (R CMD BATCH myRprogram.R)

Be sure to wrap your plots like this

pdf(file="myoutput.pdf")
plot (x,y)
dev.off()

or image wrappers

How to run R script from C# code?

There are multiple R executables. For running a R script in batch mode you will want to use Rscript.exe. It is located in the bin/ subfolder of your R installation directory.

The first argument is the .R file to execute, additional arguments can be supplied. All arguments are available to your R-Script via calling the commandArgs() function.

Note that there exists R.NET which is also available as NuGet-package. This library allows you to directly interact with the R intepreter from C#. You can also exchange data diretcly.

Run R script using JAVA program

You just want to call an external application: wouldn't the following work?

Runtime.getRuntime().exec("Rscript myScript.R"); 

Credit goes to stackoverflow itself

Is there a way to save R source code to a file programmatically?

Provided you are working in RStudio, you can use rstudioapi::documentSave. Here's an example

library(rstudioapi)
version = 1
documentSave(getActiveDocumentContext())

How do I stop/end/halt a script in R?

As far as I could find, there is no single command that really stops a script on every platform/version. There are several ways to handle this:

Put it in a function or curly brackets:

{
if (TRUE) {stop("The value is TRUE, so the script must end here")}

print("Script did NOT end!")
}

OR evaluate the error and handle it like in an if else construction:

if (TRUE) {stop("The value is TRUE, so the script must end here")    
} else { #continue the script
print("Script did NOT end!")
}

OR (EDIT):
Another possibility is to call the script from a seperate 'main' R-scipt, with source("MyScript.R"). Then the script terminates. This however, suppresses all output other then errors to the console.

OR for more complex operations, use tryCatch() as shown here



Related Topics



Leave a reply



Submit