Script source for dividing the environment in R rather than the global environment

Is there a way to source()script in R so that it is attached as a parent to the global environment ( .GlobalEnv)?

Currently, when I run a script, all the variables and functions of this script appear in my global (interactive) environment. I would like to include these variables and functions in the search path, but not in .GlobalEnv. That is, I would like the source code script to behave like an attached package that is tied between the global and the base environments (see Figure in Advanced R Environments )

enter image description here

+6
source share
4 answers

source local , , .

, , source, local, attach .

attach what=NULL , local source:

tmp <- attach(what=NULL)
source('test.R', local=tmp)

:

source('test.R', local=attach(NULL))
+3

script, (.. , , , R script), , .BaseNamespaceEnv, source() .

, script, :

# << my-script.R >>
my_fun <- function(x){x + y}

, , , , my_fun :

source("my-script.R")
y = 2
my_fun(1)
#> 3

, , search() (.GlobalEnv), script:

# Create the environment:
ENV = new.env(parent = .BaseNamespaceEnv)
# Attache it to the search path so that objects in your environment can be
# found from the global environment (i.e. from the console):
attach(ENV)
# do things:
source("my-script.R",ENV)
y = 2
my_fun(1)
#> Error in .ENV$my_fun(3) : object 'y' not found
+3

, , , :

:

search()
# [1] ".GlobalEnv"        "package:stats"     "package:graphics"
# [4] "package:grDevices" "package:utils"     "package:datasets"
# [7] "package:methods"   "Autoloads"         "package:base"

local, source() ing:

attach(new.env(), name="sourced_scripts")
myEnv <- as.environment("sourced_scripts")

source("some_other_script.R", local=myEnv)

search()
#  [1] ".GlobalEnv"        "package:dplyr"     "sourced_scripts"
#  [4] "package:stats"     "package:graphics"  "package:grDevices"
#  [7] "package:utils"     "package:datasets"  "package:methods"
# [10] "Autoloads"         "package:base"

script dplyr , , "package:dplyr" script.

dplyr ( ), "sourced_script" , , script, : attach() , attach() ( myEnv).

detach("sourced_scripts")
parent.env(myEnv) <- parent.env(.GlobalEnv)
parent.env(.GlobalEnv) <- myEnv
rm(myEnv)  # at this point we can remove myEnv to clear up namespace

search()
#  [1] ".GlobalEnv"        "sourced_scripts"   "package:dplyr"
#  [4] "package:stats"     "package:graphics"  "package:grDevices"
#  [7] "package:utils"     "package:datasets"  "package:methods"
# [10] "Autoloads"         "package:base"
+2
source

I would also like to use a solution with a function . Using and arguments allows you to conveniently (IMHO) bypass the global environment. According to related documentation: sys.source sys.sourceenvirtoplevel.env

sys.source[p] sys.sourceexpressions in this file, and then sequentially evaluates them in the specified environment.

tstEnv <- new.env()
sys.source(file = "tst.R", envir = tstEnv, toplevel.env = tstEnv)

where contains: tst.R

a <- 1
b <- 1

Results:

ls(envir = .GlobalEnv)
# [1] "tstEnv"
ls(envir = tstEnv)
# [1] "a" "b"
tstEnv$a
# [1] 1
+2
source

Source: https://habr.com/ru/post/1655355/


All Articles