r - Using lapply() in renderUI() in Shiny Module -
i trying convert section of code shiny module, renderplot() functions generated within lapply() don't seem working. have created simple example below demonstrate issue.
(note: here i'm using rendertext() calls, same behavior applies.)
app_normal.r:
library(shiny) ui <- fixedpage( h2("normal example"), uioutput("test") ) server <- function(input, output, session) { output$test <- renderui({ lapply(1:3, function(val) { fluidrow(column(12,rendertext(paste("line", val)))) }) }) } shinyapp(ui, server) app_module.r:
library(shiny) mymoduleui <- function(id) { ns <- ns(id) uioutput(ns("test")) } mymodule <- function(input, output, session) { output$test <- renderui({ lapply(1:3, function(val) { fluidrow(column(12,rendertext(paste("line", val)))) }) }) } ui <- fixedpage( h2("module example"), mymoduleui("test_module") ) server <- function(input, output, session) { callmodule(mymodule, "test_module") } shinyapp(ui, server) all of div elements being created, fail contain plots/text. how use shiny rendertext() or renderplot() functions within renderui()/lapply() calls within module?
it appears approach taking using rendertext() , renderplot() functions directly in renderui() works fine in normal case, i.e. when not operating within shiny module. shiny automatically calls necessary textoutput() or plotoutput() generate html. how automatic link broken when performing same operations within shiny module. suspect due mismatch between assigning , referencing items in output list due introduction of ns() call when assigning outputids done manually in call outputplot() or outputtext().
to use renderui within shiny module, need separately call textoutput() , rendertext(): textoutput in lapply() in renderui(), , rendertext() in lapply() in observe(). allows introduce ns() generation of outputid textoutput() call.
below i've included refactoring of both app_normal.r , app_module.r demonstrates disentanglement of these 2 calls.
app_normal_observe.r:
library(shiny) ui <- fixedpage( h2("normal example"), uioutput("test") ) server <- function(input, output, session) { output$test <- renderui({ lapply(1:3, function(val) { fluidrow(column(12,textoutput(paste0("line_", val)))) }) }) observe({ lapply(1:3, function(val) { output[[paste0("line_", val)]] <- rendertext(paste("line", val)) }) }) } shinyapp(ui, server) app_module_observe.r:
library(shiny) mymoduleui <- function(id) { ns <- ns(id) uioutput(ns("test")) } mymodule <- function(input, output, session) { output$test <- renderui({ lapply(1:3, function(val) { fluidrow(column(12,textoutput(session$ns(paste0("line_", val))))) }) }) observe({ lapply(1:3, function(val) { output[[paste0("line_", val)]] <- rendertext(paste("line", val)) }) }) } ui <- fixedpage( h2("module example"), mymoduleui("test_module") ) server <- function(input, output, session) { callmodule(mymodule, "test_module") } shinyapp(ui, server)
Comments
Post a Comment