r - How to correct namespace for renderText in nested shiny modules? -
i cannot find way use output's variable in rendertext in nested ui module.
the problem came when tried grab name of tab , put in header title of navbarpage layout.
i not find fix issue in answers explaining how deal renderui(xxinput()) via session$ns like one. rendertext doesn't have id argument, don't know if apply anyway.
minimal example:
library(shiny) header_module <- function(id, label="my header") { ns <- ns(id) headerpanel( div( h2("some text", shiny::br()), #h2(textoutput("tab-name")), h2(textoutput(ns("tab-name"))), h2("some more static text") ) ) } ui <- function(id, label="main ui") { ns <- ns(id) navbarpage(title = "my app", tabpanel(title = "my tab 1", pagewithsidebar( header_module(ns("header")), sidebarpanel(h2("sidebar")), mainpanel(h2("main panel"), textoutput("tab-name"))), value = "tab1"), id = ns("active-tab")) } server <- function(input, output, session) { output[["tab-name"]] <- rendertext({ input[["mainui-active-tab"]] }) } shinyapp(ui=ui("mainui"),server=server) the reference "active-tab" works in main panel, not in module-created header. if use textoutput(ns("tab-name")) (like above), empty div created <div id="mainui-header-tab-name" class="shiny-text-output shiny-bound-output"></div>. if use textoutput("tab-name"), main panel goes empty well.
it seems problem in code doesn't have modules. here fixed version:
library(shiny) header_module <- function(id, label="my header") { ns <- ns(id) headerpanel( div( h2("some text", shiny::br()), h2(textoutput("tab-name2")), #h2(textoutput(ns("tab-name2"))), h2("some more static text") ) ) } ui <- function(id, label="main ui") { ns <- ns(id) navbarpage(title = "my app", tabpanel(title = "my tab 1", pagewithsidebar( header_module(ns("header")), sidebarpanel(h2("sidebar")), mainpanel(h2("main panel"), textoutput("tab-name"))), value = "tab1"), id = ns("active-tab")) } server <- function(input, output, session) { output[["tab-name"]] <- rendertext({ input[["mainui-active-tab"]] }) output[["tab-name2"]] <- rendertext({ input[["mainui-active-tab"]] }) } shinyapp(ui=ui("mainui"),server=server) the issue output-id tab-name appeared twice in ui not valid in html documents , therefore not valid in shiny. things more complicated of course if outputs defined within modules.
remark: avoid ids containing minus symbol tab-name, since - used seperator modules already. also, syntax output$tab-name throw errors object "name" not found.

Comments
Post a Comment