7

Is there a way to run a R script using Unity's C# in Mono?

If there is no way to run an R script using Mono, I am willing to use .NET

Update

So the following code will call the R script but will not output a file if called from unity monodevelop. It would be ok to return a string to mono but changing the true and false on startInfo.UseShellExecute and startInfo.RedirectStandardOutput throws an error. Here is the C# code that will call the R code:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "Rscript";
startInfo.WorkingDirectory = Application.dataPath + "/myUnity/Scripts/";
startInfo.Arguments = "Rexp1.R";
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = true;
startInfo.RedirectStandardOutput = false;
process.StartInfo = startInfo;
process.Start();

I know for sure that the R script will output a file or I can grab stdout and hold onto it. I will be happy with outputting to a file or having unity allow for a string to be returned that is the output of the R script.

Update 2* - Here is the R script.

sink("output.txt")
nts <- matrix(rnorm(100), nrow = 500)
ds <- dist(nts,method = "euclidean", diag = TRUE, upper=TRUE)
dm <- as.matrix(ds)  # distance matrix
print(dm)
sink()
11
  • I know that r.Net uses a framework that is too high for Unity Commented Oct 27, 2016 at 20:19
  • Why are you even trying to do this? For a plugin or you just want to make your game with R? Commented Oct 27, 2016 at 20:29
  • 1
    I would like to use some functions built into R. R is a very powerful scientific research language and it could be used to shorthand many operations. I suppose you could use it for a game but I am doing research. Commented Oct 27, 2016 at 20:31
  • Yes, but not directly like you can in C or C++. You can but this is not worth it as it may not work on mobile devices. Look here, here and here Commented Oct 27, 2016 at 20:42
  • 1
    If it will work then that work be great. I never intend on supporting mobile devices. Commented Oct 27, 2016 at 20:59

1 Answer 1

5
+50

Just read from the output of the process like what you have done.

I don't have a MAC computer, but it should be the same. See comments in the code.

Your R script has error on my machine, so it output to stderr instead of stdout.

using UnityEngine;

public class RunR : MonoBehaviour
{
    void Start ()
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        // For macOS, here should be
        //     I. "/bin/sh"
        //     II. "path_of_the_Rscript"
        process.StartInfo.FileName = @"E:\Program Files\R\R-3.3.2\bin\x64\Rscript.exe";
        // For macOS, here should be
        //     I. "-c path_of_the_Rscript Rexp1.R" if "/bin/sh" is used
        //     II. "Rexp1.R" if "path_of_the_Rscript" is used
        process.StartInfo.Arguments = "Rexp1.R";
        process.StartInfo.WorkingDirectory = Application.dataPath;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.Start();
        //read the output
        string output = process.StandardOutput.ReadToEnd();
        string err = process.StandardError.ReadToEnd();
        process.WaitForExit();
        Debug.Log(output);
        Debug.LogError(err);
    }
}

Ouput:

Ouput

Note the project view. There are a Rexp1.R and a RunR.cs. The first output is Object, because there is no output from stdout and thus output is null.

After I changed the content of Rexp1.R to the following,

print("12345")
print("ABCDE")

the output in console view becomes:

normal output

UPDATE:

After installing the igraph package and removing sink("output.txt") and the last sink(), the output is:

possible correct output

Sign up to request clarification or add additional context in comments.

3 Comments

The R script threw an error because you needed to install the igraph library. The command to install the library can be added to the code or ran from the R command terminal : 'install.packages("igraph")'. I am still checking everything else but this looks like it should work. In any case, Thanks!
Yep, I was able to get this working. It took me a minute because I forget to add the '/' before the folder path in the workingdirectory assignment. process.StartInfo.WorkingDirectory = Application.dataPath + "/Scripts". After I found that, everything was good. Thanks for the help!
Just a side note for anyone who stumbles upon this answer trying to run R code through Unity. The method here could be used to run python, C, C++, or any number of other programming language scripts. This is incredibly helpful and is a cross platform (OSX or Windows) solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.