用户登录
用户注册

分享至

从 .net 调用 R(编程语言)

  • 作者: 一阵风刮没了
  • 来源: 51数据库
  • 2023-02-13

问题描述

我正在开发一个应用程序,该应用程序需要大量统计处理并将其输出为 .net 桌面应用程序中的图像.这些问题,包括生成输出图像,似乎很适合 R http://www.r-project.组织/

I'm working on an application that requires a great deal of stastical processing and output as images in a .net desktop application. The problems, including generating the output images, seem like a natural fit for R http://www.51sjk.com/Upload/Articles/1/0/343/343166_20230213091720254.jpg

是否有允许我从 .net 调用 R 的包装器、API、SDK 或端口?

Is there a wrapper, API, SDK, or port that will allow me to call R from .net?

推荐答案

R.NET 在 R.NET 的较新版本中存在相当大的问题.如果它不能正常工作,它会工作得非常糟糕(并且会继续这样做,除非你知道如何修复它).

R.NET is pretty buggy with the newer version of R. And if it doesn't work right, it works terribly (and will continue to do so unless you know exactly how to fix it).

就我个人而言,我建议使用 R 脚本文件并执行它们.你应该做的是用

Personally, I'd recommend using R script files and executing them. What you should do is start your R script with

> sink()
> #set your working directory here with setwd()
> #your code comes in here
> sink(#name your output file here - could label it with a .txt if you please
+ )

从 .NET 开始,您必须通过键入 using System.Diagnostics 来包含 System.Diagnostics 命名空间,然后编写以下代码:

And from .NET, you have to include the System.Diagnostics namespace by typing using System.Diagnostics and then write this code:

string strCmdLine;
strCmdLine = "R CMD BATCH" + /* the path to your R script goes here */;
System.Diagnostics.Process.Start("CMD.exe",strCmdLine);
process1.Close();

然后您可以像这样使用 StreamReader:

You can then use a StreamReader like this:

StreamReader ROutput = new StreamReader(/* your R output file's path should go here */)

然后根据需要解析它(如果您也需要帮助,请参阅 RegEx 和字符串的 split 方法).

And then parse it as you please (see RegEx and a string's split method if you need help with that too).

希望这有帮助!

软件
前端设计
程序设计
Java相关