从命令行运行 R 代码 (Windows) [英] Running R Code from Command Line (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.
我该如何实现?
推荐答案
你想要
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()
.
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屋!