0

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?

Kun Ren
  • 4,715
  • 3
  • 35
  • 50
  • This may be a duplicate of http://stackoverflow.com/q/9871307/602276 – Andrie Sep 26 '14 at 06:57
  • Why should one do this? With [R.NET](https://rdotnet.codeplex.com/) there exists a really great interface between C# and other .net dialects and R. You can easily interchange not only strings and scalars, but also vectors, matrices, lists, ... Have a look at this great piece of work and have fun :-) – Patrick Roocks Sep 26 '14 at 08:49
  • Thanks @PatrickRoocks, I deliberately avoid using R.NET in this case for some reason, just want to know a minimal case to interact with an R session through stdio, I think I've solved the problem already. Thanks anyway! – Kun Ren Sep 26 '14 at 08:57

1 Answers1

2

The answer in https://stackoverflow.com/a/9370949/2906900 works for this question.

Here's an minimal example in which C# and rscript.exe interact through stdio.

In the R script, stdin connection must be explicitly open.

R code:

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.

C# code:

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);
        }
    }
}
Community
  • 1
  • 1
Kun Ren
  • 4,715
  • 3
  • 35
  • 50