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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5025340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:16:36  来源:igfitidea点击:

Call R (programming language) from .net

.netrinteropstatisticscompatibility

提问by detroitpro

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.r-project.org/

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

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

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

回答by Moriarty Snarly

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.NET 在 R 的较新版本上有很多问题。如果它不能正常工作,它会工作得非常糟糕(并且会继续这样做,除非您确切地知道如何修复它)。

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

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

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

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

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

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

You can then use a StreamReader like this:

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

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

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

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

Hope this helps!

希望这可以帮助!

回答by Thomas Bratt

I found this library easier to use:

我发现这个库更容易使用:

http://rdotnet.codeplex.com/

http://rdotnet.codeplex.com/

Some reasons why:

一些原因:

  • Only a single .NET assembly is required
  • The DCOM Server actually requires several components from different places
  • One of the components has a very restrictive license. Only direct downloads from the website are allowed - no other form of distribution is permitted, by default, which is going to make deployment interesting
  • 只需要一个 .NET 程序集
  • DCOM 服务器实际上需要来自不同地方的几个组件
  • 其中一个组件具有非常严格的许可证。只允许从网站直接下载 - 默认情况下不允许其他形式的分发,这将使部署变得有趣

回答by Contango

For the record, if you want to call C# from R, check out rClr at https://rclr.codeplex.com/.

作为记录,如果您想从 R 调用 C#,请在https://rclr.codeplex.com/ 上查看 rClr 。

回答by Stefan Steiger

As other people said, R.NET is pretty buggy. To shield yourselfs from effects of new R versions, you can use R-server (rserve) here.

正如其他人所说,R.NET 有很多问题。为了保护自己免受新 R 版本的影响,您可以在此处使用R-server (rserve)

You can then use a rserve-client to execute your R scripts.
On sourceforge, you will find the C# version.

然后您可以使用 rserve-client 来执行您的 R 脚本。
在 sourceforge 上,您将找到 C# 版本

You will need R 1.5+ installed on the server.

您需要在服务器上安装 R 1.5+。

回答by Andrey K.

Shinyis an option.

闪亮是一种选择。

You can run a shiny app and open your report both in your browser and your wpfbrowser control.

您可以运行闪亮的应用程序并在浏览器和wpf浏览器控件中打开报告。

Here is a more detailed explanation. It is in other language, but I tried to insert English keywords. All italictext is English.

这里有更详细的解释。它是其他语言的,但我尝试插入英文关键字。所有斜体文字都是英文。

Example in other language but with English keywords

其他语言的示例,但带有英文关键字