【问题标题】:Run R script with start.process in .net在 .net 中使用 start.process 运行 R 脚本
【发布时间】:2013-08-14 06:15:22
【问题描述】:

我想在 vb.net 中运行一个 r 脚本,它将数据保存在 .csv 文件中。到目前为止,我发现了以下方法:

dim strCmd as String
strCmd = "R CMD BATCH" + "C:\test.R"
process.start("CMD.exe", strCmd

显然这应该有效,但在我的情况下,cmd 弹出(位于我的调试文件夹中)并且没有任何反应。

我尝试的另一种方法是

process.start("Rgui.exe", "C:\test.R")

错误消息:参数“C:\test.R”被忽略

这也不起作用。

对于我的 r 脚本,我只是使用了一个示例

sink()
setwd("C:/")
x <- data.frame(a = I("a \" quote"), b = pi)
sink(test.csv)

【问题讨论】:

  • 这会导致 strCmd 成为“R CMD BATCHC:\test.R”吗?
  • 嗯,这很有帮助。我将其更改为“strCmd =”R CMD BATCH”+“”+“C:/test.R”。cmd 打开和关闭没有错误,但没有创建 test.csv。你知道为什么这没有发生吗?
  • 您是要输入sink("test.csv") 吗?也许用sink() 关闭那个文件?
  • 我做了更改,但仍然没有得到输出文件。
  • 如果您没有得到预期的输出,那么您应该在test.r.Rout 中查找错误消息。它应该看起来像控制台会话的记录。

标签: .net r process.start


【解决方案1】:

这对我来说是这样的:

        Dim myprocess As New Process
        myprocess.StartInfo = New ProcessStartInfo("C:\Program Files (x86)\R\R-2.6.2\bin\R.exe", "CMD BATCH C:/Users/test/test3.R C:/Users/test/testout.csv")
        myprocess.Start()

第二种方法(第一个应用程序。没有给我一个好的 csv 输出,所以这是一个解决方法):

    Dim proc = New Process
    proc.StartInfo.FileName = "C:\Program Files (x86)\R\R-2.6.2\bin\Rscript.exe"
    proc.StartInfo.WorkingDirectory = "C:\Program Files (x86)\R\R-2.6.2\bin"
    proc.StartInfo.Arguments = "C:/Users/Desktop/RtoVB/test4.r"
    proc.StartInfo.UseShellExecute = True
    proc.StartInfo.RedirectStandardOutput = False
    proc.Start()

【讨论】:

  • 您可以使用 Rscript.exe 代替 R CMD BATCH,它用于运行脚本文件。此外,2.6.2 版现在已经过时了。
  • 我试过 "myprocess.StartInfo = New ProcessStartInfo("C:\Program Files (x86)\R\R-2.6.2\bin\Rscript.exe", C:/Users/test/ test3.R C:/Users/test/testout.csv")" 但是这不起作用,没有错误,没有创建输出文件。我怎样才能用 rscript.exe 完成这个?
  • @Jake GetDirectoryName 在哪里定义? GetDirectoryName(rScriptExecutablePath) 出现错误。由于我对 C# 和 R 都不熟悉,因此无法修复错误。
【解决方案2】:

这是我最近为此目的编写的课程。您还可以从 C# 和 R 传入和返回参数:

/// <summary>
/// This class runs R code from a file using the console.
/// </summary>
public class RScriptRunner
{
    /// <summary>
    /// Runs an R script from a file using Rscript.exe.
    /// Example:  
    ///   RScriptRunner.RunFromCmd(curDirectory + @"\ImageClustering.r", "rscript.exe", curDirectory.Replace('\\','/'));
    /// Getting args passed from C# using R:
    ///   args = commandArgs(trailingOnly = TRUE)
    ///   print(args[1]);
    /// </summary>
    /// <param name="rCodeFilePath">File where your R code is located.</param>
    /// <param name="rScriptExecutablePath">Usually only requires "rscript.exe"</param>
    /// <param name="args">Multiple R args can be seperated by spaces.</param>
    /// <returns>Returns a string with the R responses.</returns>
    public static string RunFromCmd(string rCodeFilePath, string rScriptExecutablePath, string args)
    {
            string file = rCodeFilePath;
            string result = string.Empty;

            try
            {

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

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

                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("R Script failed: " + result, ex);
            }
    }
}

【讨论】:

  • 嗨!我让它与 'MyR.RunRScript("C:\myRScript.R", "C:\Rscript.exe", "9999 4 5 66 string")' 一起工作,但我没有得到工作的结果。不管我怎么尝试 MsgBox(result) 总是空的。你知道为什么或者我做错了什么吗?
  • 我可以得到如下结果:
  • 字符串 scriptPath = @"C:\Users\Jake\Desktop\RunR\rscript.txt";字符串结果 = RScriptRunner.RunFromCmd(scriptPath, "rscript.exe", ""); Console.WriteLine(结果); Console.ReadKey();
  • 但是,结果只会捕获 R 返回到 R 控制台的数据。如果你想要一个文件的结果,你将在 R 中发出命令来将你的数据写入文件。
  • 例如,这会将数据返回到 R 中的“结果”:test
猜你喜欢
  • 2019-03-09
  • 1970-01-01
  • 2020-06-23
  • 2018-09-02
  • 2018-09-30
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多