通过RScripRunner类从c#Gui中使用RGL Plott执行.R脚本

时间:2018-10-30 07:17:00

标签: c# .net r rgl r.net

我有一个带有XYZ-Data的PointCloud,并希望通过我的C#GUI中的.r脚本进行绘制。因此,我在R中使用“ rgl”库。R代码工作正常。我尝试使用此类:

/// <summary>
/// This class runs R code from a file using the console.
/// </summary>
public class RScriptRunner
{
    /// <summary>
    /// Runs an R script from a file using Rscript.exe.
    /// Example:  
    ///   RScriptRunner.RunFromCmd(curDirectory + @"\ImageClustering.r", "rscript.exe", curDirectory.Replace('\\','/'));
    /// Getting args passed from C# using R:
    ///   args = commandArgs(trailingOnly = TRUE)
    ///   print(args[1]);
    /// </summary>
    /// <param name="rCodeFilePath">File where your R code is located.</param>
    /// <param name="rScriptExecutablePath">Usually only requires "rscript.exe"</param>
    /// <param name="args">Multiple R args can be seperated by spaces.</param>
    /// <returns>Returns a string with the R responses.</returns>
    public static string RunFromCmd(string rCodeFilePath, string rScriptExecutablePath, string args)
    {
            string file = rCodeFilePath;
            string result = string.Empty;

            try
            {

                var info = new ProcessStartInfo();
                info.FileName = rScriptExecutablePath;
                info.WorkingDirectory = Path.GetDirectoryName(rScriptExecutablePath);
                info.Arguments = rCodeFilePath + " " + args;

                info.RedirectStandardInput = false;
                info.RedirectStandardOutput = true;
                info.UseShellExecute = false;
                info.CreateNoWindow = true;

                using (var proc = new Process())
                {
                    proc.StartInfo = info;
                    proc.Start();
                    result = proc.StandardOutput.ReadToEnd();
                    proc.Close();
                }

                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("R Script failed: " + result, ex);
            }
    }
}

我从https://stackoverflow.com/questions/18224439/run-r-script-with-start-process-in-net/53054499#53054499复制了它

问题是当我从上方通过RScriptRunner-Class启动.r文件时,rgl-plot弹出并直接关闭。那么该怎么办才能使绘图窗口保持打开状态? 这是我的.R代码:

daten <- read.csv("file:///C:\\Users\\Quirin\\Dropbox\\KwiJuLa\\Testmessung Debugging 25.10\\test.csv")

library(rgl)

datenMatrix <- as.matrix(daten)
x1 <- datenMatrix[, 3]
y1 <- datenMatrix[, 4]
z1 <- datenMatrix[, 5]


rgl1 <- plot3d(x1, y1, z1)

亲切的问候

0 个答案:

没有答案