0

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)
tauft
  • 546
  • 4
  • 13

1 Answers1

0

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)
Soren
  • 1,792
  • 1
  • 13
  • 16
  • Thanks so much Soren that works. In the actual app, though, there are multiple numeric inputs for each selection: e.g., `"young" = list(numericInput("v1", "This", value = 0), numericInput("v2", "That", value = 0)), "old" = list(numericInput("v1", "The", value = 0), numericInput("v2", "Other", value = 0))` I optimistically tried: `"young" = vars <- list(numericInput("v1", "This", value = 0), numericInput("v2", "That", value = 0)),` but obviously that didn't work. Do I need to make a separate event reactive for each numeric input? – tauft Apr 09 '18 at 00:53
  • I made an event reactive for each numeric input which works. – tauft Apr 09 '18 at 03:00