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