【问题标题】:Is it possible to Run R Code from Unity C# in Mono or .NET on OSX?是否可以在 OSX 上的 Mono 或 .NET 中从 Unity C# 运行 R 代码?
【发布时间】:2016-12-14 03:36:41
【问题描述】:

有没有办法在 Mono 中使用 Unity 的 C# 运行 R 脚本?

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

更新

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

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 脚本的输出。

更新 2* - 这是R脚本。

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

【问题讨论】:

  • 我知道r.Net使用的框架对Unity来说太高了
  • 你为什么还要这样做?对于一个插件或者你只是想用 R 来制作你的游戏?
  • 我想使用R内置的一些功能。R是一种非常强大的科学研究语言,它可以用来简写许多操作。我想你可以将它用于游戏,但我正在研究。
  • 是的,但不像在 C 或 C++ 中那样直接。您可以,但这不值得,因为它可能不适用于移动设备。看hereherehere
  • 如果它行得通,那就太好了。我从不打算支持移动设备。

标签: c# r macos unity3d monodevelop


【解决方案1】:

只需像您所做的那样从流程的输出中读取。

我没有MAC电脑,但应该是一样的。见代码中的 cmets。

你的 R 脚本在我的机器上有错误,所以它输出到 stderr 而不是 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.R 和一个RunR.cs。第一个输出是Object,因为标准输出没有输出,因此输出为空。

我把Rexp1.R的内容改成如下之后,

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

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

更新:

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

【讨论】:

  • R 脚本抛出错误,因为您需要安装 igraph 库。安装库的命令可以添加到代码中或从 R 命令终端运行:'install.packages("igraph")'。我仍在检查其他所有内容,但这看起来应该可以工作。无论如何,谢谢!
  • 是的,我能够让它工作。我花了一分钟,因为我忘记在工作目录分配中的文件夹路径之前添加“/”。 process.StartInfo.WorkingDirectory = Application.dataPath + "/Scripts"。在我发现之后,一切都很好。感谢您的帮助!
  • 对于任何偶然发现此答案并试图通过 Unity 运行 R 代码的人来说,这只是一个旁注。此处的方法可用于运行 python、C、C++ 或任何数量的其他编程语言脚本。这非常有用,而且是一个跨平台(OSX 或 Windows)解决方案。
猜你喜欢
  • 1970-01-01
  • 2011-01-08
  • 2018-07-16
  • 2020-05-25
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 2020-07-15
  • 2010-09-19
相关资源
最近更新 更多