1

我的目标是调用保存在磁盘中的 R 函数,执行该函数并将输出显示到 html 页面。

为了获得这一点,我编写了以下代码(在网上某处找到)。

 public ActionResult Index()
    {
        var rCodeFilePath = @"K:/RPrograms/hello.R";

        var rScriptExecutablePath = @"C:/Program Files/R/R-3.2.2/bin/Rscript.exe";

        string result = string.Empty;

        try
        {

            var info = new ProcessStartInfo();
            info.FileName = rScriptExecutablePath;
            info.WorkingDirectory = Path.GetDirectoryName(rScriptExecutablePath);
            info.Arguments = rCodeFilePath;

            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();
                proc.Close();
            }

            ViewBag.Result=result;
        }
        catch (Exception ex)
        {
            throw new Exception("R Script failed: " + result, ex);
        }

        return View();
    }

我没有看到任何错误,但在“结果”中,我得到一个空结果。当我在“RGui”中运行该函数时,它会显示一个结果。R 函数基本上是一个 hello world 代码。

任何人都可以显示一些光?

4

1 回答 1

0

您确定这是正确的网址吗?“C:/Program Files/R/R-3.2.2/bin/Rscript.exe” 尝试使用您的 URL 指向文件,如下所示:C:\Program Files\R\R-3.2.2\bin\Rscript。可执行程序

我只是建议,虽然不确定。

祝你好运。

于 2015-09-03T10:21:00.740 回答