eval_tidy parse_quo in the parent.frame environment can not get to work

Hi,

I am trying run 'R language' in then parent.frame environment.

The R base method works using eval() and parse() and assign().
I can not find an rlang way using eval_tidy(), parse_quo() and assign().
What else to I need to do?
Thanks,
Andre

innerf <- function() {

  require(rlang)

  # rlang
  # action <- parse_expr("assign(\"envr\", environment())")
  # action <-  parse_quo("assign(\"envr\", environment())", env = parent.frame())
    action <-  parse_quo("assign(\"envr\", environment())", env = env_parent())

  # does not work         # quosures: env is ignored
  eval_tidy(quo(!!action))
                      
  # base R 
  eval(parse(text="assign(\"envo\", environment())"), envir = parent.frame())

}

outerf <- function() {

  innerf()
  print(ls())

} 

#' so, no envr is created

outerf()
[1] "envo"

I ended up doing this.

innerf <- function() {

  require(rlang)

  # rlang
  action  <- parse_expr("assign(\"envr\", environment())")
  eval_bare(action, caller_env())
                      
  # base R 
  eval(parse(text="assign(\"envo\", environment())"), envir = parent.frame())

}

outerf <- function() {

  innerf()
  print(ls())
  print(str(envo))
  print(str(envr))
  print(identical(envo,envr))

} 

outerf()
[1] "envo" "envr"
<environment: 0x00000000050c5b08>
NULL
<environment: 0x00000000050c5b08>
NULL
[1] TRUE

Help from

op_as_closure
https://github.com/cfhammill/lenses/blob/db9fcca6610b47cd186f2c5d53fb3642585770b4/R/utils.R

from

https://github.com/search?q=eval_bare+extension%3Ar

This also works

with_env(env = caller_env(), { assign("envr", environment()) })
1 Like

If your question's been answered (even if by you, as in this case), would you mind choosing a solution? (See FAQ below for how).

Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.

Thanks