RDotNet vs R scripts

When is the advantage or disadvantage of using RDotNet for statistical calculations, rather than creating an R script text file and running it from an application, for example, Process.Start? Or is there another better way?

I need to execute a large number of commands and have the feeling that sending them one by one to R takes a lot of time.

+7
c # scripting r rdotnet
source share
3 answers

I would say that the following two scenarios are stereotyped:

  • The .NET code and the R code are pretty separate, it doesn't take much interaction between the R code and the .NET code. For example, the .NET code collects some information and starts processing the script, after which the .NET code picks up the results. In this case, spawning the R-process (Process.Start) is a simple way to make it work.
  • It takes a lot of interaction between the .NET code and the R code, the workflow consists of a frequent transition between .NET and R. In this case, a heavier, more flexible solution, such as RDotNet, makes a lot of sense. RDotNet makes it easier to integrate .NET code and R code because it is often difficult to learn, harder to debug and often needs to be updated for new versions of R, etc.
+5
source share

Using Process.Start, you start a new R session. This can take some time, especially if you use different packages in the script that you need to download.

If you are using R.NET, you can create an instance of R and continue talking to it. Therefore, if you created a web service to connect R with ASP, you do not want to run R all the time, since it will be very expensive. You need it only once, and you can work with it interactively.

+2
source share

Currently, RNET can initialize once. Concurrent execution will be problematic.

Suggests using RScript.

Our solution is based on this answer on stackoverflow Calling R (programming language) from .net

With changes in the mono mode, we send the R-code from the line and save it in a temporary file, because if necessary, the user runs a custom R-code.

public static void RunFromCmd(string batch, params string[] args) { // Not required. But our R scripts use allmost all CPU resources if run multiple instances lock (typeof(REngineRunner)) { string file = string.Empty; string result = string.Empty; try { // Save R code to temp file file = TempFileHelper.CreateTmpFile(); using (var streamWriter = new StreamWriter(new FileStream(file, FileMode.Open, FileAccess.Write))) { streamWriter.Write(batch); } // Get path to R var rCore = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\R-core") ?? Registry.CurrentUser.OpenSubKey(@"SOFTWARE\R-core"); var is64Bit = Environment.Is64BitProcess; if (rCore != null) { var r = rCore.OpenSubKey(is64Bit ? "R64" : "R"); var installPath = (string)r.GetValue("InstallPath"); var binPath = Path.Combine(installPath, "bin"); binPath = Path.Combine(binPath, is64Bit ? "x64" : "i386"); binPath = Path.Combine(binPath, "Rscript"); string strCmdLine = @"/c """ + binPath + @""" " + file; if (args.Any()) { strCmdLine += " " + string.Join(" ", args); } var info = new ProcessStartInfo("cmd", strCmdLine); info.RedirectStandardInput = false; info.RedirectStandardOutput = true; info.UseShellExecute = false; info.CreateNoWindow = true; using (var proc = new Process()) { proc.StartInfo = info; proc.Start(); result = proc.StandardOutput.ReadToEnd(); } } else { result += "R-Core not found in registry"; } Console.WriteLine(result); } catch (Exception ex) { throw new Exception("R failed to compute. Output: " + result, ex); } finally { if (!string.IsNullOrWhiteSpace(file)) { TempFileHelper.DeleteTmpFile(file, false); } } } } 

Full blog post: http://kostylizm.blogspot.ru/2014/05/run-r-code-from-c-sharp.html

+2
source share

All Articles