闪亮的动态UI:无法打印使用renderUI创建的uiOutput的结果

时间:2018-04-08 23:05:53

标签: r shiny

我正在使用splashUI来创建文本字段,具体取决于用户的选择。然后用户填写字段,并在下面的简化版本中,目标是显示结果。如何从uiOutput获取数据?

我认为这个问题的答案会有所帮助:How to get the value in uioutput in ui.R and send it back to server.R?但它对我的应用无效。

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 个答案:

答案 0 :(得分:0)

我在下面修改了您的代码,似乎符合您的情况。更改是numericInput()对象分别命名为'this'和'that',并且eventReactve()对象正在侦听名为'variables'的对象。因为当值递增时,您的numericInputs()会发生变化,这就是需要监听的反应。我更改了名为“vars”的条件numericInput()对象,并在eventReactive中更改了类似的输入$ 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)