4

I am using MS Visual Studio R Tools where I can prepare and test script in R language. Then I want to start process (using Process.Start(startInfo)) from C# code to execute this script, wait until it finishes and check output. The script produces some statistical computations and saves results on hard drive in .csv file.

How do I find the path to R interpreter? Is the code below correct?

Is it possible to add command line arguments to the R script when calling it from C# code?

ProcessStartInfo startInfo = new ProcessStartInfo();
r_interpreter_path="???";
startInfo.FileName = r_interpreter_path;
startInfo.Arguments = "\"" + r_script_name + " \"";
//Add command line arguments
startInfo.Arguments += " -sd " + date_start_str + " -ed " + date_end_str;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardError = true;
using (Process process = Process.Start(startInfo))
{
    using (StreamReader reader = process.StandardOutput)
    {
        string result = reader.ReadToEnd();
        Debug.WriteLine(result);
    }
    process.WaitForExit();
    //string errMsg = process.StandardError.ReadToEnd();
    //if (errMsg != "")
    //    return false;
    GC.Collect();
}

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.