如何将参数传递给我桌面上的 Rscript? [英] How can I pass arguments to an Rscript i have in my desktop?

查看:25
本文介绍了如何将参数传递给我桌面上的 Rscript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的桌面上有一个 rscript (file.r),其中包含一个函数.我需要从 Windows 命令提示符调用这个函数并将参数传递给它,我已经找到了这种方式,但我不明白它是如何使用的,比如它是什么意思?
我已经有了 R 的外壳,但我需要从 Windows 命令提示符执行它,而不是 R 本身

I have a rscript (file.r) in my desktop which contains a function. I need to call this function from Windows command prompt and pass arguments to it, I have found this way but i don't understand how it's used, like what does it mean?
I already have the R's shell but i need to do it from Windows command prompt, not R's itself

args <- commandArgs(trailingOnly = TRUE)

推荐答案

你有你的 R 脚本 (test.R),例如:

You have your R script (test.R), for example:

#commandArgs picks up the variables you pass from the command line
args <- commandArgs(trailingOnly = TRUE)
print(args)

然后使用以下命令从命令行运行脚本:

Then you run your script from the command line using:

#here the arguments are 5 and 6 that will be picked from args in the script
PS C:\Users\TB\Documents> Rscript .\test.R 5 6
[1] "5"      "6"

然后你得到的是一个包含 2 个元素的向量,即 5 和 6.trailingOnly = TRUE 确保你只返回 5 和 6 作为参数.如果省略它,则变量 args 还将包含有关调用的一些详细信息:

Then what you get back is a vector containing 2 elements, i.e. 5 and 6. trailingOnly = TRUE makes sure you just get back 5 and 6 as the arguments. If you omit it then the variable args will also contain some details about the call:

例如检查这个.我的 R 脚本是:

Check this for example. My R script is:

args <- commandArgs()
print(args)

然后调用返回:

PS C:\Users\TB\Documents> Rscript .\test.R 5 6
[1] "C:\\Users\\TB\\scoop\\apps\\anaconda3\\current\\lib\\R\\bin\\x64\\Rterm.exe"
[2] "--slave"
[3] "--no-restore"
[4] "--file=.\\test.R"
[5] "--args"
[6] "5"
[7] "6"

我没有在此处包含 trailingOnly = TRUE,但我也返回了一些通话详细信息.

I didn't include the trailingOnly = TRUE here and I got some call details returned as well.

这篇关于如何将参数传递给我桌面上的 Rscript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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