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.
我想构建一个通过标准输入/输出流与R(rscript.exe)通信所需的C#程序。但我找不到在rscript的输入流中写任何东西的方法。
Here is the C# program that uses a Process whose streams are redirected.
这是使用其流被重定向的进程的C#程序。
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
:
这是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"
.
str能够捕获“hello”但是“hello2”不能写入R的流中,因此str2总是得到“\ r \ ninin输出:\ r \ n”。
Is there a way to write texts into R's input stream in this way?
有没有办法以这种方式将文本写入R的输入流?
1 个解决方案
#1
1
The answer in https://*.com/a/9370949/2906900 works for this question.
https://*.com/a/9370949/2906900中的答案适用于此问题。
Here's an minimal example in which C# and rscript.exe interact through stdio.
这是一个最小的例子,其中C#和rscript.exe通过stdio进行交互。
In the R script, stdin
connection must be explicitly open.
在R脚本中,必须显式打开stdin连接。
R code:
R代码:
f <- file("stdin")
open(f)
input <- readLines(f, n = 1L)
cat("input is:", input)
In this case, the input stream of rscript can be accessed.
在这种情况下,可以访问rscript的输入流。
C# code:
C#代码:
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")
{
Arguments = "script.R",
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
proc.Start();
proc.StandardInput.WriteLine("Hello");
var output = proc.StandardOutput.ReadLine();
Console.WriteLine(output);
}
}
}
#1
1
The answer in https://*.com/a/9370949/2906900 works for this question.
https://*.com/a/9370949/2906900中的答案适用于此问题。
Here's an minimal example in which C# and rscript.exe interact through stdio.
这是一个最小的例子,其中C#和rscript.exe通过stdio进行交互。
In the R script, stdin
connection must be explicitly open.
在R脚本中,必须显式打开stdin连接。
R code:
R代码:
f <- file("stdin")
open(f)
input <- readLines(f, n = 1L)
cat("input is:", input)
In this case, the input stream of rscript can be accessed.
在这种情况下,可以访问rscript的输入流。
C# code:
C#代码:
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")
{
Arguments = "script.R",
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
proc.Start();
proc.StandardInput.WriteLine("Hello");
var output = proc.StandardOutput.ReadLine();
Console.WriteLine(output);
}
}
}