RDotNet和R脚本编写有什么区别?

7

相对于生成R脚本文本文件并使用例如Process.Start从应用程序运行它,使用RDotNet进行统计计算的优缺点是什么?还是有其他更好的方法吗?

我需要执行大量命令,感觉逐个将其发送到R需要很多时间。

3个回答

6
我认为以下两种情况是刻板印象:
  1. .NET代码和R代码相当独立,不需要太多的交互。例如,.NET代码收集一些信息,并在此基础上启动处理脚本,之后.NET代码获取结果即可。在这种情况下,生成一个R进程(Process.Start)是使其工作的简单方式。
  2. 需要大量的.NET代码和R代码之间的交互,工作流程经常在.NET和R之间来回进行。在这种情况下,使用更加复杂灵活的解决方案,例如RDotNet,是非常合理的选择。RDotNet 可以更轻松地集成.NET代码和R代码,但价格是它更难学习、更难调试,并且通常需要针对新版本的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。本文内容涉及 IT 技术,介绍了如何在 C# 中运行 R 代码,并提供了详细的操作步骤。如果您对此感兴趣,可以查看原文获取更多信息。

2
使用Process.Start可以启动一个新的R会话。这可能需要一些时间,特别是如果您在脚本中使用不同的包并需要加载它们。
如果您使用R.NET,可以创建一个R实例,并继续与其交互。因此,如果您已经创建了一个用于连接ASP和R的Web服务,您不希望每次都启动R,因为这将非常耗时。您只需要启动一次即可交互地使用它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接