是否可以在OSX的Mono或.NET中从Unity C#运行R代码? [英] Is it possible to Run R Code from Unity C# in Mono or .NET on OSX?

查看:78
本文介绍了是否可以在OSX的Mono或.NET中从Unity C#运行R代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在Mono中使用Unity的C#运行R脚本?

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

如果无法使用Mono运行R脚本,我愿意使用.NET

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

更新

因此,以下代码将调用R脚本,但如果从unity monodevelop调用,则不会输出文件.可以将字符串返回单声道,但是更改startInfo.UseShellExecute和startInfo.RedirectStandardOutput的true和false会引发错误.这是将调用R代码的C#代码:

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

我确定R脚本会输出文件,或者我可以抓取stdout并保留它.我将很高兴输出到文件或统一允许返回作为R脚本输出的字符串.

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.

更新2 *- 这是R脚本.

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()

推荐答案

就像您所做的一样,仅从过程的输出中读取.

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

我没有MAC计算机,但是应该一样.查看代码中的注释.

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

您的R脚本在我的计算机上出错,因此它输出到stderr而不是stdout.

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

输出:

请注意项目视图.有一个Rexp1.RRunR.cs.第一个输出是Object,因为stdout没有输出,因此输出为null.

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.

将Rexp1.R的内容更改为以下内容后,

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

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

控制台视图中的输出变为:

the output in console view becomes:

更新:

在安装完igraph软件包并删除了sink("output.txt")和最后一个sink()之后,输出为:

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

这篇关于是否可以在OSX的Mono或.NET中从Unity C#运行R代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆