I have noticed different behavior when using env_parent()
from rlang
package and when using env_parent(caller_env())
, although caller_env()
is a default argument for env_parent()
first parameter:
library(rlang)
env_parent
#> function (env = caller_env(), n = 1)
#> {
#> env_ <- get_env_retired(env, "env_parent()")
#> while (n > 0) {
#> if (is_empty_env(env_)) {
#> abort("The empty environment has no parent")
#> }
#> n <- n - 1
#> env_ <- parent.env(env_)
#> }
#> env_
#> }
#> <bytecode: 0x000000001d8ad000>
#> <environment: namespace:rlang>
I'm not very familiar with environments and I was able to prepare MRE only based on shiny app (I have noticed this using shiny app). It is important to have app in two files - ui.R
and server.R
:
ui.R
library(shiny)
library(rlang)
parent <<- function() {
env_parent()
}
parents_default_arg_passed <<- function() {
env_parent(caller_env())
}
ui <- fluidPage(
textOutput("env_parent"),
textOutput("env_parents_default_arg_passed")
)
server.R
server <- function(input, output, session) {
output$env_parent <- renderPrint({
names(parent())
})
output$env_parents_default_arg_passed <- renderPrint({
names(parents_default_arg_passed())
})
}
I see this output after running the app:
[1] "ui"
[1] "~" ".__tidyeval_quosure_mask__."
Why is that?