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

前端 未结 5 1223
盖世英雄少女心
盖世英雄少女心 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条回答
  •  旧时难觅i
    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.

提交回复
热议问题