以编程方式执行 R 脚本
我有一个生成一些 R 代码的 C# 程序。现在,我将脚本保存到文件,然后将其复制/粘贴到 R 控制台中。我知道 R 有一个 COM 接口,但它似乎不适用于最新版本的 R(或 2.7.8 之后的任何版本)。有什么方法可以在将 R 脚本保存到文件后以编程方式从 C# 执行它吗?
I have a C# program that generates some R code. Right now I save the script to file and then copy/paste it into the R console. I know there is a COM interface to R, but it doesn't seem to work with the latest version of R (or any version after 2.7.8). Is there some way I can just programmatically execute the R script from C# after saving it to file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为 C# 有一个类似于 system() 的函数,它允许您调用通过 Rscript.exe 运行的脚本。
I would presume that C# has a function similar to
system()
which would allow you to call scripts running viaRscript.exe
.我们的解决方案基于 stackoverflow 上的这个答案 从 .net 调用 R(编程语言)
通过单一更改,我们从字符串发送 R 代码并将其保存到临时文件,因为用户在需要时运行自定义 R 代码。
完整博客文章:http://kostylizm。 blogspot.ru/2014/05/run-r-code-from-c-sharp.html
Our solution based on this answer on stackoverflow Call R (programming language) from .net
With monor change, we send R code from string and save it to temp file, since user run custom R code when needed.
Full blog post: http://kostylizm.blogspot.ru/2014/05/run-r-code-from-c-sharp.html
要在
C#
中执行此操作,您需要使用确保像这样包装您的绘图
或图像包装器
To do this in
C#
you'll need to useBe sure to wrap your plots like this
or image wrappers
这是实现这一目标的简单方法,
我的 Rscript 位于:
R 代码位于:
Here is a simple way to achieve that,
My Rscript is located at:
R Code is at :
这是我最近为此目的编写的课程。您还可以从 C# 和 R 传入并返回参数:
注意:如果您有兴趣清理进程,您可能需要将以下内容添加到代码中。
proc.CloseMainWindow();
proc.Close();
Here is the class I recently wrote for this purpose. You can also pass in and return arguments from C# and R:
NOTE: You may want to add the following to you code, if you are interested in cleaning up the process.
proc.CloseMainWindow();
proc.Close();