Hi.
I'm new to developing RShiny apps and am having trouble creating a form in which a user (1) selects a response, (2) and scores the responses by pressing the "submit" button. I've created a simplified ReprEx below.
require(shiny)
require(tidyverse)
fieldsAll <- c("nervous")
saveData <- function(data)
{
data <- as.data.frame(t(data))
if (exists("responses"))
{
responses <<- rbind(data)
} else {
responses <<- data
}
score <<-(apply(responses, 1, sum))
names(responses) <- c('Feeling nervous/on edge')
}
loadData <- function() {
if (exists("responses")) {
responses
}
}
loadScore <- function() {
if (exists("score")) {
score
}
}
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),
textOutput("score"),
),
),
fluidRow(
actionButton("submit", "Submit")
),
fluidRow(
DT::dataTableOutput("responses", width = 200),
tags$hr()
)
)
server <- function(input, output, session) {
# Whenever a field is filled, aggregate all form data
formData <- reactive({
data <- sapply(fieldsAll, function(x) input[[x]])
data
})
# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
saveData(formData())
})
# Show the previous responses
# (update with current response when Submit is clicked)
output$responses <- DT::renderDataTable({
input$submit
loadData()
},
options=list(dom = 't',
ordering = FALSE)
)
}
shinyApp( ui = ui, server = server)
Thanks in advance.
Ethan