【问题标题】:Run Rscript using ProcessStartInfo from C#使用 C# 中的 ProcessStartInfo 运行 Rscript
【发布时间】:2019-10-01 22:44:10
【问题描述】:

在执行下面的代码时,我在结果变量中没有得到预期值 (8)?我哪里错了?代码执行没有任何错误,但结果变量的值将在整个过程中为空

R code:(Script.R)
AddTwo<-function(firstNumber, SecondNumber)
{
Total<-firstNumber+SecondNumber
Total
}

AddTwo(5,3)

我已经在代码中给出的路径中安装了 R 和 RScript.exe

class Program
{
    static void Main(string[] args)
    {
        var rpath = @"C:\Program Files\R\R-3.6.0\bin\Rscript.exe";
        var scriptpath = @"E:\Projects\Cash Forecast\Documents\Payroll\Script.R";
        var output = RunFromCmd(scriptpath, rpath);

        Console.WriteLine(output);
        Console.ReadLine();
    }

    public static string RunFromCmd(string rCodeFilePath, string rScriptExecutablePath)
    {
        string file = rCodeFilePath;
        string result = string.Empty;

        try
        {

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


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

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

【问题讨论】:

  • 尝试用CMD中的参数执行exe,然后看看输出是什么。可能是 cmd 参数没有输出,代码正确但参数不正确。
  • 我已经更新了代码。这里 arg 是虚拟值。我的函数没有明确地取值。
  • 我用批处理文件运行了相同的 r 函数,它给出了输出。此处给出了批处理代码: cd "C:\Users\kumarky\Desktop\out" "C:\Program Files\R\R-3.6.0\bin\R.exe" CMD BATCH "E:\Projects\Cash Forecast \Documents\Payroll\Script.R" "C:\Users\kumarky\Desktop\out\abc.Rout"
  • 在脚本路径中删除“现金预测”之间的空间时它起作用了。有什么办法可以保留空间吗?
  • string file = rCodeFilePath 没有被使用,所以我看不出会有什么不同。您可以通过将值括在引号 (") 中来为值添加空格。

标签: c# rscript processstartinfo


【解决方案1】:

帖子很旧,仍然共享修复,因为它可以帮助其他人,在 R 脚本的命令行执行期间,如果任何路径有空格,则应添加引号,如下所示

public static string AddQuotesIfRequired(string path)
        {
            return !string.IsNullOrWhiteSpace(path) ?
                path.Contains(" ") && (!path.StartsWith("\"") && !path.EndsWith("\"")) ?
                    "\"" + path + "\"" : path :
                    string.Empty;
        }

【讨论】:

    【解决方案2】:

    我相信你需要等待程序运行。

    proc.WaitForExit();
    

    程序运行,但没有时间在 C# 程序的其余部分继续之前获得响应。

    更多信息可以在这里找到:

    https://support.microsoft.com/en-gb/help/305369/how-to-wait-for-a-shelled-application-to-finish-by-using-visual-c

    【讨论】:

    • 添加但没有运气! proc.StartInfo = 信息; proc.Start(); proc.WaitForExit();结果 = proc.StandardOutput.ReadToEnd();
    猜你喜欢
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 2019-01-08
    相关资源
    最近更新 更多