0

I would like to know if I can use interchangeably getDefaultReactiveDomain() with session object as a parameter in the server function. In other words: is getDefaultReactiveDomain() a function which returns R6 object and session is just this R6 object which is returned by getDefaultReactiveDomain()?

Let's say, I would like to write a function to reload a web page and I want it to work even if user doesn't include session parameter in server function:

reload <- function() {
  getDefaultReactiveDomain()$reload()
}

Can I do it safely? I think that I do not see generally usage of getDefaultReactiveDomain()$ on Internet (speaking broadly), but session$, so I'm afraid it could be not so easy as I think.

MRE:

getDefaultReactiveDomain

library(shiny)
library(magrittr)

reload <- function() {
  getDefaultReactiveDomain()$reload()
}

ui <- fluidPage(
  actionButton("reload", "Reload")
)

server <- function(input, output) {
  observe({
    reload()
  }) %>%
    bindEvent(input$reload)
}

shinyApp(ui, server)

session

library(shiny)
library(magrittr)

ui <- fluidPage(
  actionButton("reload", "Reload")
)

server <- function(input, output, session) {
  observe({
    session$reload()
  }) %>%
    bindEvent(input$reload)
}

shinyApp(ui, server)

I would like to add that in the shiny source code I see returned value from getDefaultReactiveDomain() as a default value passed to the session parameter, for example in this function:

shiny::updateActionButton
#> function (session = getDefaultReactiveDomain(), inputId, label = NULL, 
#>     icon = NULL) 
#> {
#>     validate_session_object(session)
#>     if (!is.null(icon)) 
#>         icon <- as.character(validateIcon(icon))
#>     message <- dropNulls(list(label = label, icon = icon))
#>     session$sendInputMessage(inputId, message)
#> }
#> <bytecode: 0x000000001dbdc2a0>
#> <environment: namespace:shiny>

So I could be safe using just getDefaultReactiveDomain() in my function (and using it internally, not as a default value for parameter, which user can change). However, if that's true, why then in update* functions from shiny other value can be passed to session parameter?

gss
  • 1,334
  • 6
  • 11
  • 2
    This is because of shiny modules. Inside modules, session domain is different than the top-level session's domain. By default `updateXX` uses current domain, but inside modules, this gives you the option to pass parent session. – lz100 Jan 12 '22 at 18:37
  • 1
    This [question](https://stackoverflow.com/questions/51708815/accessing-parent-namespace-inside-a-shiny-module) shows exactly how to do what @lz100 explained. – jpdugo17 Jan 13 '22 at 06:04

0 Answers0