How I can pass parameter of one file R to other file R using source()?




I am using the source () to open one file R but I need pass some parameters to this file and I don’t know how do.

Can you help me?


Hi @sue,

do you have one example of what you are trying to do ?

Know that when using source, it just execute code in your environment as if you would have done interactively. Let’s say I have a script foo.R than have a variable in it foo_var that is not define inside the script. If I do

foo_var <- "a value"

it will work because before the source command I define the variable in my global environment and source is executed using this environment.

Better solution for that are using functions. I could create inside foo.R a function foo_func <- function(foo_var){...}. Then I could do

foo_func(foo_var = "a value")

That way, sourcing the script load the function in the global environment than I can pass an argument to this function.

Know that you can also create some script as executable and use inside commandArgs() to make same read some argument. But it is another use case maybe.

Hopes this hints will help you precise your question.

Notes: I see you categorize your question in package development but know that in a package, you won’t need to source another file.