I want to build a C# program that is needed to communicate with R (rscript.exe
) via standard input/output stream. But I can't find a way to write anything into rscript's input stream.
Here is the C# program that uses a Process whose streams are redirected.
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace test1
{
class Program
{
static void Main(string[] args)
{
var proc = new Process();
proc.StartInfo = new ProcessStartInfo("rscript", "script.R")
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false
};
proc.Start();
var str = proc.StandardOutput.ReadLine();
proc.StandardInput.WriteLine("hello2");
var str2 = proc.StandardOutput.ReadToEnd();
}
}
}
Here is script.R
:
cat("hello\n")
input <- readline()
cat("input is:",input, "\n")
str
is able to capture "hello"
but "hello2"
can't be written into R's stream so that str2
always gets "\r\ninput is: \r\n"
.
Is there a way to write texts into R's input stream in this way?