Executing R script programmatically

后端 未结 5 858
臣服心动
臣服心动 2020-12-09 18:31

I have a C# program that generates some R code. Right now I save the script to file and then copy/paste it into the R console. I know there is a COM interface to R, but it d

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 18:45

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

    With monor change, we send R code from string and save it to temp file, since user run custom R code when needed.

    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

提交回复
热议问题