我的目标是调用保存在磁盘中的 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 代码。
任何人都可以显示一些光?