I would probably do it like this:
require(shiny)
require(tidyverse)
fieldsAll <- c("nervous","other")
resp.options <- c('Not at all' = 0, 'Several days' = 1, 'More than half the days' = 2, 'Nearly every day' = 3)
ui <- fluidPage(
titlePanel("Flawed Example"),
fluidRow(
column(6,
selectInput("nervous", "Over the last 2 weeks, how much have you been bothered by feeling nervous or on edge?",
resp.options),
selectInput("other", "Over the last 2 weeks, how much have you been bothered by feeling nervous or on edge?",
resp.options),
textOutput("score"),
),
),
fluidRow(
actionButton("submit", "Submit")
),
fluidRow(
DT::dataTableOutput("responses", width = 200),
tags$hr()
)
)
server <- function(input, output, session) {
formLog <- reactiveVal(NULL)
# Whenever a field is filled, aggregate all form data
formData <- reactive({
map(fieldsAll,~input[[.x]]) %>%
set_names(fieldsAll) %>%
as_tibble(data)
})
# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
formLog(
bind_rows(formLog(),formData()))
})
table_to_show <- eventReactive(input$submit, {
formLog()
})
# Show the previous responses
# (update with current response when Submit is clicked)
output$responses <- DT::renderDataTable({
req(table_to_show())
},
options=list(#dom = 't',
ordering = FALSE)
)
}
shinyApp( ui = ui, server = server)