2015-09-03 55 views
1

我的目標是調用保存在磁盤中的R函數,執行該函數並將輸出顯示爲html頁面。從Asp.net調用R腳本mvc

爲了獲得這個,我寫了下面的代碼(在網上找到的)。

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世界代碼。

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

回答

0

您確定這是正確的網址嗎? 「C:/ Program Files/R/R-3.2.2/bin/Rscript.exe」 嘗試使用你的URL到這樣的文件:C:\ Program Files \ R \ R-3.2.2 \ bin \ Rscript .exe

我只是建議,但不知道。

Goodluck更進一步。

+0

我用那個試過..沒有運氣 – kandroid

+0

如果你試圖訪問計算機上的那些程序/目錄,它們確實存在嗎?有時候這只是一個錯字,我也不確定。 –

+0

:)是的,他們這樣做。 – kandroid