Source into a specified environment

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Source into a specified environment

G.Maubach
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_export_table_2_xls.R",
             "t_find_duplicates_in_variable.R",
"t_find_originals_and_duplicates.R",
             "t_frequencies.R", "t_inspect_dataset.R",
"t_merge_variables.R",
             "t_openxlsx_shortcuts.r", "t_rename_variables.R",
"t_select_chunks.R")

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

for (file in modules)
{
  source(file = file.path(
    c_path_full$modules,  # path to modules
    file),
    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))
{
  do.call(
    what = "source",
    args = list(
      file = file.path(c_path_full$modules,
                       file),
      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
work


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

Kind regards

Georg

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Source into a specified environment

Ivan Calandra-5
Hi Georg,

Not sure how it would work in your case, but the 'local' argument to
source() is not only TRUE or FALSE; you can also specify an environment.

HTH,
Ivan

--
Ivan Calandra, PhD
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
[hidden email]
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra
https://rgzm.academia.edu/IvanCalandra
https://publons.com/author/705639/

Le 09/01/2017 à 17:21, [hidden email] a écrit :

> 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_export_table_2_xls.R",
>               "t_find_duplicates_in_variable.R",
> "t_find_originals_and_duplicates.R",
>               "t_frequencies.R", "t_inspect_dataset.R",
> "t_merge_variables.R",
>               "t_openxlsx_shortcuts.r", "t_rename_variables.R",
> "t_select_chunks.R")
>
> toolbox <- new.env(parent = emptyenv())
>
> for (file in modules)
> {
>    source(file = file.path(
>      c_path_full$modules,  # path to modules
>      file),
>      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))
> {
>    do.call(
>      what = "source",
>      args = list(
>        file = file.path(c_path_full$modules,
>                         file),
>        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
> work
>
>
> Is there a way to load the modules directly into the "toolbox"
> environment?
>
> Kind regards
>
> Georg
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Source into a specified environment

jholtman
In reply to this post by G.Maubach
?sys.source

Here is an example of the way I use it:

# read my functions into a environment
.my.env.jph <- new.env()
.sys.source('~/C_Drive/perf/bin/perfmon.r', envir=.my.env.jph)
attach(.my.env.jph)


Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Mon, Jan 9, 2017 at 11:21 AM, <[hidden email]> wrote:

> 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_export_table_2_xls.R",
>              "t_find_duplicates_in_variable.R",
> "t_find_originals_and_duplicates.R",
>              "t_frequencies.R", "t_inspect_dataset.R",
> "t_merge_variables.R",
>              "t_openxlsx_shortcuts.r", "t_rename_variables.R",
> "t_select_chunks.R")
>
> toolbox <- new.env(parent = emptyenv())
>
> for (file in modules)
> {
>   source(file = file.path(
>     c_path_full$modules,  # path to modules
>     file),
>     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))
> {
>   do.call(
>     what = "source",
>     args = list(
>       file = file.path(c_path_full$modules,
>                        file),
>       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
> work
>
>
> Is there a way to load the modules directly into the "toolbox"
> environment?
>
> Kind regards
>
> Georg
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

SOLVED: Re: Source into a specified environment

G.Maubach
Hi Jim,

many thanks for your answer.

That's exactly what I need.

Many thanks again.

Kind regards

Georg




Von:    jim holtman <[hidden email]>
An:     [hidden email],
Kopie:  R mailing list <[hidden email]>
Datum:  10.01.2017 03:59
Betreff:        Re: [R] Source into a specified environment



?sys.source

Here is an example of the way I use it:

# read my functions into a environment
.my.env.jph <- new.env()
.sys.source('~/C_Drive/perf/bin/perfmon.r', envir=.my.env.jph)
attach(.my.env.jph)


Jim Holtman
Data Munger Guru
 
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Mon, Jan 9, 2017 at 11:21 AM, <[hidden email]> wrote:
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_export_table_2_xls.R",
             "t_find_duplicates_in_variable.R",
"t_find_originals_and_duplicates.R",
             "t_frequencies.R", "t_inspect_dataset.R",
"t_merge_variables.R",
             "t_openxlsx_shortcuts.r", "t_rename_variables.R",
"t_select_chunks.R")

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

for (file in modules)
{
  source(file = file.path(
    c_path_full$modules,  # path to modules
    file),
    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))
{
  do.call(
    what = "source",
    args = list(
      file = file.path(c_path_full$modules,
                       file),
      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
work


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

Kind regards

Georg

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.