Change the environment of a formula to a specified environment.
retarget(x, env = parent.frame())
The formula or list of formulas with the new environment set.
# A rather inefficient way to build a formula... for the sake of the demo!
make_formula <- function(x)
as.formula(x)
f <- make_formula("x ~ log(y) + z")
f
#> x ~ log(y) + z
#> <environment: 0x55af6cacfa50>
f <- retarget(f)
f
#> x ~ log(y) + z
#> <environment: 0x55af6cb45b78>
# OK, but the environment associated to this formula is...
# the environment of the function:
rlang::f_env(f)
#> <environment: 0x55af6cb45b78>
# With a list of formulas (local() creates a new environment):
fl <- local(list(y ~x^2, z~ sqrt(cos(x^2) + sin(x^2))))
fl # Not in GlobalEnv
#> [[1]]
#> y ~ x^2
#> <environment: 0x55af6c8a2c58>
#>
#> [[2]]
#> z ~ sqrt(cos(x^2) + sin(x^2))
#> <environment: 0x55af6c8a2c58>
#>
retarget(fl) # Retargeted to GlobalEnv
#> [[1]]
#> y ~ x^2
#> <environment: 0x55af6cb45b78>
#>
#> [[2]]
#> z ~ sqrt(cos(x^2) + sin(x^2))
#> <environment: 0x55af6cb45b78>
#>