Consider any Shiny module in which we use session$ns
in the server part, e.g. the one below.
We could equivalently use NS(id)
instead of session$ns
. So why it is recommended to use session$ns
? Is there an advantage over NS(id)
?
library(shiny)
myModuleUI <- function(id){
ns <- NS(id)
uiOutput(ns("checkbox"))
}
myModuleServer <- function(id){
moduleServer(id, function(input, output, session){
ns <- session$ns
output[["checkbox"]] <- renderUI({
checkboxInput(ns("checkbox"), label = "Check me", value = FALSE)
})
observe({
print(input[["checkbox"]])
})
})
}
ui <- basicPage(
myModuleUI("myModule")
)
server <- function(input, output, session){
myModuleServer("myModule")
}
shinyApp(ui, server)