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

夙愿已清 提交于 2020-01-06 04:33:27

问题


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)

回答1:


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)


来源:https://stackoverflow.com/questions/49723638/dynamic-ui-in-shiny-cant-print-results-from-uioutput-created-with-renderui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!