简体   繁体   中英

Dynamic UI in shiny: Can't print results from uiOutput created with renderUI

I'm using renderUI in shiny to create text fields depending on the user's selection. The user then fills out the fields and in the simplified version below, the goal is to display the results. How can I get the data from uiOutput?

I thought the answer to this question would help: How to get the value in uioutput in ui.R and send it back to server.R? but it doesn't work for my app.

library(shiny)
ui <- fluidPage(
  tabsetPanel(
     tabPanel("data", fluid = TRUE,
         sidebarLayout(                                                                                     
           sidebarPanel(selectInput(inputId = "age2", label = "Select", choices = c("young", "old")), 
                        actionButton("goButton", "Go!"), 
                        uiOutput("variables")),
           mainPanel(verbatimTextOutput("print"))))
    )
  )
server <- function(input, output, session) {
  output$variables <- renderUI({
    switch(input$age2, 
           "young" = numericInput("this", "This", value = NULL),
           "old" = numericInput("that", "That", value = NULL)
    ) 
  })
  x <- eventReactive(input$goButton, {
  input$variables
  })
  output$print <- renderText({
    x()
  })
} 
shinyApp(ui = ui, server = server)

I modified your code below and seems to meet your case. The change is that the numericInput() objects were respectively named 'this' and 'that' and that your eventReactve() object was listening to object named 'variables'. As it is your numericInputs() that are changing when the value is incremented, this is what reactive needs to listen to. I changed both the conditional numericInput() objects named as "vars" and in eventReactive to similarly listen to input$vars

library(shiny)
ui <- fluidPage(
  tabsetPanel(
    tabPanel("data", fluid = TRUE,
             sidebarLayout(                                                                                     
               sidebarPanel(selectInput(inputId = "age2", label = "Select", choices = c("young", "old")), 
                            actionButton("goButton", "Go!"), 
                            uiOutput("variables")),
               mainPanel(verbatimTextOutput("print"))))
  )
)
server <- function(input, output, session) {
  output$variables <- renderUI({
    switch(input$age2, 
           "young" = numericInput("vars", "This", value = NULL),
           "old" = numericInput("vars", "That", value = NULL)
    ) 
  })
  x <- eventReactive(input$goButton, {
    input$vars

  })
  output$print <- renderText({
    x()
  })
} 
shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM