【问题标题】:RDotNet vs R scriptingRDotNet 与 R 脚本
【发布时间】:2013-08-06 00:31:30
【问题描述】:

什么时候使用 RDotNet 进行统计计算而不是生成 R 脚本文本文件并使用例如从应用程序运行它是一种优势/劣势。进程.开始?还是有其他更好的方法?

我需要执行大量的命令,感觉将它们一个一个发送到R需要很多时间。

【问题讨论】:

    标签: c# r scripting r.net


    【解决方案1】:

    我想说以下两种情况是典型的:

    1. .NET 代码和 R 代码是完全分开的,R 代码和 .NET 代码之间不需要太多交互。例如,.NET 代码收集一些信息,并在其上启动一个处理脚本,之后 .NET 代码获取结果。在这种情况下,生成一个 R 进程 (Process.Start) 是实现此功能的一种简单方法。
    2. .NET 代码和 R 代码之间需要大量交互,工作流包括经常在 .NET 和 R 之间来回切换。在这种情况下,像 RDotNet 这样重量更重、更灵活的解决方案就很有意义了。 RDotNet 允许更轻松地集成 .NET 代码和 R 代码,其代价是它通常更难学习、更难调试,并且经常需要针对新版本的 R 等进行更新。

    【讨论】:

      【解决方案2】:

      使用 Process.Start 您将启动一个新的 R 会话。这可能需要一些时间,尤其是当您在脚本中使用需要加载的不同包时。

      如果你使用 R.NET,你可以创建一个 R 实例,并继续与之对话。因此,如果您创建了一个将 R 与 ASP 连接起来的 Web 服务,您就不想一直启动 R,因为这将非常耗时。您只需要它一次,就可以交互式地使用它。

      【讨论】:

        【解决方案3】:

        R.NET 目前可以初始化一次。并行执行会有问题。

        建议使用 RScript。

        我们的解决方案基于stackoverflow上的这个答案Call R (programming language) from .net

        随着单声道变化,我们从字符串发送 R 代码并将其保存到临时文件,因为用户在需要时运行自定义 R 代码。

        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);
                    }
                }
            }
        }
        

        完整博文:http://kostylizm.blogspot.ru/2014/05/run-r-code-from-c-sharp.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-28
          • 1970-01-01
          • 1970-01-01
          • 2021-09-19
          • 1970-01-01
          • 1970-01-01
          • 2021-12-14
          • 2022-12-10
          相关资源
          最近更新 更多