1

On executing below code I dont get expected value (8) in result variable? Where am I going wrong? Code executes without any error but value of the result variable will be blank throughout

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

AddTwo(5,3)

I have installed R and RScript.exe present at path given in code

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);
         }
    }
}
Kuntady Yathish
  • 49
  • 2
  • 10
  • Try executing the exe with the arguments in CMD, then see what the output is. IT may be that the cmd arguments dont have an output and the code is correct but the arguments are not. – Christopher Vickers May 14 '19 at 21:06
  • I have updated the code. and here arg is dummy value. my function do not take values explicitly. – Kuntady Yathish May 14 '19 at 21:19
  • I ran same r function with batch file and it gives the out put. Batch code is given here: 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" – Kuntady Yathish May 14 '19 at 21:21
  • On Removing Space between "Cash Forecast" in scriptpath it worked. Is there any work around to retain space? – Kuntady Yathish May 15 '19 at 05:31
  • string file = rCodeFilePath is not used so i cant see how that would make a difference. You can add spaces to values by enclosing them in quotation marks ("). – Christopher Vickers May 16 '19 at 10:21

2 Answers2

1

I believe you need to wait for the program to run.

proc.WaitForExit();

The program runs but does not have time to get a response before the rest of your C# program continues.

More information can be found here:

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

Christopher Vickers
  • 1,773
  • 1
  • 14
  • 18
0

Post is old, still sharing the fix as it can help others, during command line execution for R script, if any path is having space then quotes should be added like below

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