Call R (programming language) from .net

时光总嘲笑我的痴心妄想 提交于 2019-11-26 12:23:54

问题


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/

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


回答1:


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

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

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

You can then use a StreamReader like this:

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

Hope this helps!




回答2:


I found this library easier to use:

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



回答3:


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




回答4:


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.

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

You will need R 1.5+ installed on the server.




回答5:


Shiny is an option.

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

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

Example in other language but with English keywords



来源:https://stackoverflow.com/questions/5025340/call-r-programming-language-from-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!