从命令行运行 R 代码 (Windows) [英] Running R Code from Command Line (Windows)

查看:30
本文介绍了从命令行运行 R 代码 (Windows)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为 analysis.r 的文件中有一些 R 代码.我希望能够从命令行 (CMD) 运行该文件中的代码而不必通过 R 终端,我还希望能够传递参数并在我的代码中使用这些参数像下面的伪代码:

I have some R code inside a file called analyse.r. I would like to be able to, from the command line (CMD), run the code in that file without having to pass through the R terminal and I would also like to be able to pass parameters and use those parameters in my code, something like the following pseudocode:

C:>(execute r script) analyse.r C:file.txt

这将执行脚本并将C:file.txt"作为参数传递给脚本,然后它可以使用它对其进行进一步处理.

and this would execute the script and pass "C:file.txt" as a parameter to the script and then it could use it to do some further processing on it.

我该如何实现?

推荐答案

  1. 你想要Rscript.exe.

您可以从脚本内控制输出——请参阅 sink() 及其文档.

You can control the output from within the script -- see sink() and its documentation.

您可以通过 commandArgs() 访问命令参数.

You can access command-arguments via commandArgs().

您可以通过 getoptoptparse 包.

You can control command-line arguments more finely via the getopt and optparse packages.

如果其他一切都失败了,请考虑阅读手册贡献文档

If everything else fails, consider reading the manuals or contributed documentation

这篇关于从命令行运行 R 代码 (Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆