October 22, 2024
Chicago 12, Melborne City, USA
HTML

Unable to access inputs from Shiny modules when using templates


I’m trying to access inputs within Shiny modules where the UI is defined using HTML templates, but I can’t seem to access the input values from the server-side of the module. I’m not sure if I’m missing any steps when working with templates. Below is a simple reproducible Shiny app that demonstrates the issue:

library(shiny)

# Defining the templates
template1 <- '<label for="input2">Enter second value:</label>
  <input type="text" id="input2" class="shiny-bound-input" />'

template2 <- '<label for="input3">Enter third value:</label>
  <input type="text" id="input3" class="shiny-bound-input" />'

# Module 1 UI and Server
mod1_ui <- function(id) {
  ns <- NS(id)
  uiOutput(ns("mod1"))
}

mod1_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    
    observe({
      print(input$input2)  # Trying to access input2 here
    })
    
    output$mod1 <- renderUI({
      HTML(template1)  # Rendering the template with input2
    })
  })
}

# Module 2 UI and Server
mod2_ui <- function(id) {
  ns <- NS(id)
  uiOutput(ns("mod2"))
}

mod2_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    output$mod2 <- renderUI({
      HTML(template2)
    })
  })
}

# Main UI
ui <- fluidPage(
  mod1_ui("mod1"),  # Module 1 UI
  mod2_ui("mod2")   # Module 2 UI
)

# Main Server
server <- function(input, output, session) {
  mod1_server("mod1")  # Module 1 Server
  mod2_server("mod2")  # Module 2 Server
}

# Running the app
shinyApp(ui, server)

In this example, I’m trying to access the value of input2 inside mod1_server, but it seems like input$input2 doesn’t exist. Is there a special procedure I need to follow to access inputs when using HTML templates in Shiny modules? Any help would be appreciated!



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video