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

shiny

#1

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

I thought the answer to this SO question would help: https://stackoverflow.com/questions/34459837/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)

#2

This was the answer:


#3

If your question's been answered (even elsewhere/by you in the link), would you mind choosing it as your solution? (see FAQ below for how) It makes it a bit easier to visually navigate the site and see which questions still need help.

Thanks