[R] Source into a specified environment

G.Maubach at weinwolf.de G.Maubach at weinwolf.de
Mon Jan 9 17:21:03 CET 2017

Hi All,

I wish everyone a happy new year.

I have the following code:

-- cut --

modules <- c("t_calculate_RFM_model.R", "t_count_na.R", 
             "t_frequencies.R", "t_inspect_dataset.R", 
             "t_openxlsx_shortcuts.r", "t_rename_variables.R", 

toolbox <- new.env(parent = emptyenv())

for (file in modules)
  source(file = file.path(
    c_path_full$modules,  # path to modules
    echo = TRUE)

-- cut --

I would like to know how I can source the modules into the newly created 
environment called "toolbox"?

I had a look at the help file for ?source but this function can read in 
only in the current environment or the global environment (= default).

I tried also the following

-- cut --

for (file in modules))
    what = "source",
    args = list(
      file = file.path(c_path_full$modules,
      echo = TRUE
    envir = toolbox

-- cut --

But this did not work, i. e. it did not load the modules into the 
environment "toolbox" but into the .GlobalEnv.

I also had a look at "assign", but assign() askes for a name of an object 
in quotes. This way I could not figure out how to use it in a loop or 
function to name the element in "toolbox" after the modules names:

assign("t_add_sheet", t_add_sheet, envir = toolbox)  # works
assign(quote(t_add_sheet), t_add_sheet, envir = toolbox)  # does NOT work
assign(as.name(t_add_sheet), t_add_sheet, envir = toolbix)  # does NOT 

Is there a way to load the modules directly into the "toolbox" 

Kind regards


More information about the R-help mailing list