是否可以 运行 在 OSX 上使用 Mono 或 .NET 中的 Unity C# R 代码?
Is it possible to Run R Code from Unity C# in Mono or .NET on OSX?
有没有办法 运行 在 Mono 中使用 Unity 的 C# 编写 R 脚本?
如果没有办法运行使用Mono的R脚本,我愿意使用.NET
更新
所以下面的代码将调用 R 脚本,但如果从 unity monodevelop 调用,则不会输出文件。可以将 return 字符串转换为单声道,但在 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 并保留它。我会很高兴输出到文件或统一允许 returned 作为 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()
只需像您所做的那样从过程的输出中读取。
我没有MAC电脑,不过应该是一样的。请参阅代码中的注释。
你的 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
,因为 stdout 没有输出,因此输出为空。
我把Rexp1.R的内容改成下面这样后,
print("12345")
print("ABCDE")
控制台视图中的输出变为:
更新:
安装 igraph 包并删除 sink("output.txt")
和最后一个 sink()
后,输出为:
有没有办法 运行 在 Mono 中使用 Unity 的 C# 编写 R 脚本?
如果没有办法运行使用Mono的R脚本,我愿意使用.NET
更新
所以下面的代码将调用 R 脚本,但如果从 unity monodevelop 调用,则不会输出文件。可以将 return 字符串转换为单声道,但在 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 并保留它。我会很高兴输出到文件或统一允许 returned 作为 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()
只需像您所做的那样从过程的输出中读取。
我没有MAC电脑,不过应该是一样的。请参阅代码中的注释。
你的 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
,因为 stdout 没有输出,因此输出为空。
我把Rexp1.R的内容改成下面这样后,
print("12345")
print("ABCDE")
控制台视图中的输出变为:
更新:
安装 igraph 包并删除 sink("output.txt")
和最后一个 sink()
后,输出为: