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
Difference Between Casting and Using the Convert.To() Method
How to Display Formatted Code in Webpage
Signalr(V2.2.0) Ondisconnected Set User Offline
How to Use Java-Style Throws Keyword in C#
Are Static Methods Thread Safe
Data Binding in Wpf User Controls
Is There Really Any Way to Uniquely Identify Any Computer at All
Raise Event in High Resolution Interval/Timer
Implicit VS Explicit Interface Implementation
What Is the Purpose of Anonymous { } Blocks in C Style Languages
Remove CSS Class in Code Behind
Unity: Apple Game Center iOS7 Registers the Scores for Only 30 Minutes in the Leaderboard
What Are Major Differences Between C# and Java
How Does MVC 4 List Model Binding Work