Home > Software design >  sliderInput with renderUI returns NULL in modularized R Shiny app
sliderInput with renderUI returns NULL in modularized R Shiny app

Time:02-22

I have a modularized R Shiny app in which a sliderInput() is created within renderUI() so that the value can be passed to further processing (here, a renderPrint()). First, I tested the construct in a simple app composed of a global.R, a ui.R and a server.R, where it works as expected (i.e. on slider modification, the correct slider input is printed):

global.R

library(shiny)

ui.R

shinyUI(fluidPage(
  fluidRow(column(3, 
                  uiOutput("ui")),
           column(3,
                  verbatimTextOutput("dynamic_value")))))

server.R

shinyServer(function(input, output, session) {
  
  output$ui <- renderUI({
    sliderInput("dynamic", "The slider:",
                min = 1, 
                max = 20,
                value = 10)
  })
  
  output$dynamic_value <- renderPrint({
    str(input$dynamic)
  })
  
})

Then, I transferred this construct to a modularized app, in which--to my surprise--the value passed from the slider to the text output is NULL instead of the actual value from the slider input:

global.R

library(shiny)

# load module
source("test_module.R")

test_ui.R

test_module_ui(id = "test")

ui.R

shinyUI(fluidPage(navbarPage(title = "Dynamic sliderInput",
                             tabPanel("Panel",
                                      source("test_ui.R",
                                             local = TRUE)$value))))

server.R

shinyServer(function(input, output, session) {
  callModule(test_module, "test")
  })

test_module.R

test_module_ui <- function(id) {
  ns <- NS(id) # namespace
  
  fluidRow(
    column(3, 
           uiOutput(ns("ui"))),
    column(3,
           verbatimTextOutput(ns("dynamic_value")))
    )
}

test_module <- function(input, output, session) {
  output$ui <- renderUI({
    sliderInput("dynamic", "The slider:",
                min = 1, 
                max = 20,
                value = 10)
  })
  
  output$dynamic_value <- renderPrint({
    str(input$dynamic)
  })
}

Does someone know what is misspecified here? Any hints are highly appreciated--thank you! :)

CodePudding user response:

As stated in this question, you have to use session$ns() in the renderUI():

test_module_server <- function(input, output, session) {
  output$ui <- renderUI({
    sliderInput(session$ns("dynamic"), "The slider:",
                min = 1, 
                max = 20,
                value = 10)
  })
  • Related