当源文件()为R文件时,如何传递命令行参数

时间:2022-12-10 23:15:15

In one R file, I plan to source another R file that supports reading two command-line arguments. This sounds like a trivia task but I couldn't find a solution online. Any help is appreciated.

在一个R文件中,我计划编写另一个R文件,该文件支持读取两个命令行参数。这听起来像是一个琐碎的任务,但我在网上找不到解决方案。任何帮助都是感激。

4 个解决方案

#1


30  

I assume the sourced script accesses the command line arguments with commandArgs? If so, you can override commandArgs in the parent script to return what you want when it is called in the script you're sourcing. To see how this would work:

我假定源脚本访问命令行参数与commandArgs?如果是这样,您可以在父脚本中重写commandArgs,以便在正在查找的脚本中调用它时返回所需的内容。看看这是如何运作的:

file_to_source.R

file_to_source.R

print(commandArgs())

main_script.R

main_script.R

commandArgs <- function(...) 1:3
source('file_to_source.R')

outputs [1] 1 2 3

输出[1]123

If your main script doesn't take any command line arguments itself, you could also just supply the arguments to this script instead.

如果您的主脚本本身不接受任何命令行参数,那么您也可以为这个脚本提供参数。

#2


18  

The simplest solution is to replace source() with system() and paste. Try

最简单的解决方案是用system()和粘贴替换source()。试一试

arg1 <- 1
arg2 <- 2
system(paste("Rscript file_to_source.R",arg1, arg2))

#3


3  

If you have one script that sources another script, you can define variables in the first one that can be used by the sourced script.

如果您有一个脚本可以提供另一个脚本,那么您可以在第一个脚本中定义变量,这些变量可以由源脚本使用。

> tmpfile <- tempfile()
> cat("print(a)", file=tmpfile)
> a <- 5
> source(tmpfile)
[1] 5

#4


1  

An extended version of @Matthew Plourde's answer. What I usually do is to have an if statement to check if the command line arguments have been defined, and read them otherwise.

一个扩展版本的@Matthew Plourde的答案。我通常做的是使用if语句检查命令行参数是否已定义,并读取其他参数。

In addition I try to use the argparse library to read command line arguments, as it provides a tidier syntax and better documentation.

此外,我尝试使用argparse库来读取命令行参数,因为它提供了更整洁的语法和更好的文档。

file to be sourced

文件来源

 if (!exists("args")) {
         suppressPackageStartupMessages(library("argparse"))
         parser <- ArgumentParser()
         parser$add_argument("-a", "--arg1", type="character", defalt="a",
               help="First parameter [default %(defult)s]")
         parser$add_argument("-b", "--arg2", type="character", defalt="b",
               help="Second parameter [default %(defult)s]")
         args <- parser$parse_args()
 }

file calling source()

文件调用源()

args$arg1 = "c" 
args$arg2 = "d"
source ("file_to_be_sourced.R")
print (args)

c, d

c,d

#1


30  

I assume the sourced script accesses the command line arguments with commandArgs? If so, you can override commandArgs in the parent script to return what you want when it is called in the script you're sourcing. To see how this would work:

我假定源脚本访问命令行参数与commandArgs?如果是这样,您可以在父脚本中重写commandArgs,以便在正在查找的脚本中调用它时返回所需的内容。看看这是如何运作的:

file_to_source.R

file_to_source.R

print(commandArgs())

main_script.R

main_script.R

commandArgs <- function(...) 1:3
source('file_to_source.R')

outputs [1] 1 2 3

输出[1]123

If your main script doesn't take any command line arguments itself, you could also just supply the arguments to this script instead.

如果您的主脚本本身不接受任何命令行参数,那么您也可以为这个脚本提供参数。

#2


18  

The simplest solution is to replace source() with system() and paste. Try

最简单的解决方案是用system()和粘贴替换source()。试一试

arg1 <- 1
arg2 <- 2
system(paste("Rscript file_to_source.R",arg1, arg2))

#3


3  

If you have one script that sources another script, you can define variables in the first one that can be used by the sourced script.

如果您有一个脚本可以提供另一个脚本,那么您可以在第一个脚本中定义变量,这些变量可以由源脚本使用。

> tmpfile <- tempfile()
> cat("print(a)", file=tmpfile)
> a <- 5
> source(tmpfile)
[1] 5

#4


1  

An extended version of @Matthew Plourde's answer. What I usually do is to have an if statement to check if the command line arguments have been defined, and read them otherwise.

一个扩展版本的@Matthew Plourde的答案。我通常做的是使用if语句检查命令行参数是否已定义,并读取其他参数。

In addition I try to use the argparse library to read command line arguments, as it provides a tidier syntax and better documentation.

此外,我尝试使用argparse库来读取命令行参数,因为它提供了更整洁的语法和更好的文档。

file to be sourced

文件来源

 if (!exists("args")) {
         suppressPackageStartupMessages(library("argparse"))
         parser <- ArgumentParser()
         parser$add_argument("-a", "--arg1", type="character", defalt="a",
               help="First parameter [default %(defult)s]")
         parser$add_argument("-b", "--arg2", type="character", defalt="b",
               help="Second parameter [default %(defult)s]")
         args <- parser$parse_args()
 }

file calling source()

文件调用源()

args$arg1 = "c" 
args$arg2 = "d"
source ("file_to_be_sourced.R")
print (args)

c, d

c,d