How to pass command-line arguments when calling source() on an R file within another R file

前端 未结 5 1205
盖世英雄少女心
盖世英雄少女心 2020-11-29 20:33

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

相关标签:
5条回答
  • 2020-11-29 20:50

    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.

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

    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

    0 讨论(0)
  • 2020-11-29 20:56

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

    arg1 <- 1
    arg2 <- 2
    system(paste("Rscript file_to_source.R", arg1, arg2))
    
    0 讨论(0)
  • 2020-11-29 20:58

    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:

    file_to_source.R

    print(commandArgs())
    

    main_script.R

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

    outputs [1] 1 2 3

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

    0 讨论(0)
  • 2020-11-29 21:06

    This works:

    # source another script with arguments
    source_with_args <- function(file, ...){
      commandArgs <<- function(trailingOnly){
        list(...)
      }
      source(file)
    }
    
    source_with_args("sourcefile.R", "first_argument", "second_argument")
    

    Note that the built in commandArgs function has to be redefined with <<- instead of <-. As I understand it, this makes its scope extend beyond the function source_with_args() where it is defined.

    0 讨论(0)
  • 2020-11-29 21:08

    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
    
    0 讨论(0)
提交回复
热议问题