resetting multiple checkboxes in shiny

I am creating a questionnaire in shiny and I would like the options for the questionnaires to reset after I hit submit. I have used the respective commands within a reset function. I am having problems with the items that require the same command: updateSelectInput and updateCheckboxInput. When I hit reset it only updates the first items that have the updateSelectInput and updateCheckboxInput commmands and the subsequent items do not reset when I hit submit. I have tried to remedy this but can't seem to find a solution in the documentation. I pasted my code if anyone has any solutions it would be greatly appreciated.


library(shiny)
library(ggplot2)

outputDir <- "responses"

# Define the fields we want to save from the form
fields <- c("ID",
            "date_evaluation",
            "DSFMAX",
            "DSF1AP",
            "DSF1AT",
            "DSF2AP",
            "DSF2AT",
            "DSF3AP",
            "DSF3AT",
            "DSF4AP",
            "DSF4AT",
            "DSF5AP",
            "DSF5AT",
            "DSF6AP",
            "DSF6AT",
            "DSF7AP",
            "DSF7AT",
            "DSF8AP",
            "DSF8AT",
            "DSF9AP",
            "DSF9AT",
            "DSF10AP",
            "DSF10AT",
            "DSF1BP",
            "DSF1BT",
            "DSF2BP",
            "DSF2BT",
            "DSF3BP",
            "DSF3BT",
            "DSF4BP",
            "DSF4BT",
            "DSF5BP",
            "DSF5BT",
            "DSF6BP",
            "DSF6BT",
            "DSF7BP",
            "DSF7BT",
            "DSF8BP",
            "DSF8BT",
            "DSF9BP",
            "DSF9BT",
            "DSF10BP",
            "DSF10BT")

saveData <- function(input) {
    # put variables in a data frame
    data <- data.frame(matrix(nrow=1,ncol=0))
    for (x in fields) {
        var <- input[[x]]
        if (length(var) > 1 ) {
            # handles lists from checkboxGroup and multiple Select
            data[[x]] <- list(var)
        } else {
            # all other data types
            data[[x]] <- var
        }
    }
    data$submit_time <- date()
    
    # Create a unique file name
    fileName <- sprintf(
        "%s_%s.rds", 
        as.integer(Sys.time()), 
        digest::digest(data)
    )
    
    # Write the file to the local system
    saveRDS(
        object = data,
        file = file.path(outputDir, fileName)
    )
}

loadData <- function() {
    # read all the files into a list
    files <- list.files(outputDir, full.names = TRUE)
    
    if (length(files) == 0) {
        # create empty data frame with correct columns
        field_list <- c(fields, "submit_time")
        data <- data.frame(matrix(ncol = length(field_list), nrow = 0))
        names(data) <- field_list
    } else {
        data <- lapply(files, function(x) readRDS(x)) 
        
        # Concatenate all data together into one data.frame
        data <- do.call(rbind, data)
    }
    
    data
}

# Define questions

#participant id
ID <- textInput("ID", "Please enter participant ID number")

#date evaluation took place
date_evaluation <- dateInput(
    "date_evaluation", 
    "Enter the date evaluation was completed",
    min = "2021-01-01", max = "2028-12-31", startview="year"
    # right answer is 1983-02-26
)



#max 
DSFMAX <- selectInput(
    "DSFMAX", 
    "Max Digit span forward", 
    c( "0",
        "1", 
      "2", 
      "3", 
      "4",
      "5",
      "6",
      "7",
      "8",
      "9",
      "10",
      "11"
    )
)

#1A

DSF1AP <- checkboxGroupInput(
    "DSF1AP", 
    "1A. 1-7 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)



DSF1AT <- selectInput(
    "DSF1AT", 
    "1A. 1-7 Number Correct", 
    c( "0",
        "1", 
       "2"
    )
)

#2A

DSF2AP <- checkboxGroupInput(
    "DSF2AP", 
    "2A. 5-8-2 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF2AT <- selectInput(
    "DSF2AT", 
    "2A. 5-8-2 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3"
    )
)


#3a
DSF3AP <- checkboxGroupInput(
    "DSF3AP", 
    "3A. 6-4-3-9 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)

DSF3AT <- selectInput(
    "DSF3AT", 
    "3A. 6-4-3-9 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4"
    )
)

#4a

DSF4AP <- checkboxGroupInput(
    "DSF4AP", 
    "4A. 4-2-7-3-1 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF4AT <- selectInput(
    "DSF4AT", 
    "4A. 4-2-7-3-1 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5"
    )
)

#5a

DSF5AP <- checkboxGroupInput(
    "DSF5AP", 
    "5A. 6-1-9-4-7-3 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF5AT <- selectInput(
    "DSF5AT", 
    "5A. 6-1-9-4-7-3 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5",
       "6"
    )
)

#6a


DSF6AP <- checkboxGroupInput(
    "DSF6AP", 
    "6A. 5-9-1-7-4-2-8 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF6AT <- selectInput(
    "DSF6AT", 
    "6A. 5-9-1-7-4-2-8 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5",
       "6",
       "7"
    )
)

#7a
DSF7AP <- checkboxGroupInput(
    "DSF7AP", 
    "7A. 5-8-1-9-2-6-4-7 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF7AT <- selectInput(
    "DSF7AT", 
    "7A. 5-8-1-9-2-6-4-7 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5",
       "6",
       "7",
       "8"
    )
)

#8a

DSF8AP <- checkboxGroupInput(
    "DSF8AP", 
    "8A. 2-7-5-8-6-2-5-8-4 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF8AT <- selectInput(
    "DSF8AT", 
    "8A. 2-7-5-8-6-2-5-8-4 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5",
       "6",
       "7",
       "8",
       "9"
    )
)

#9a

DSF9AP <- checkboxGroupInput(
    "DSF9AP", 
    "9A. 3-6-5-1-9-4-7-2-8-5 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF9AT <- selectInput(
    "DSF9AT", 
    "9A. 3-6-5-1-9-4-7-2-8-5 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5",
       "6",
       "7",
       "8",
       "9",
       "10"
    )
)


#10a

DSF10AP <- checkboxGroupInput(
    "DSF10AP", 
    "10A. 5-2-8-6-9-1-7-4-8-3-9 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF10AT <- selectInput(
    "DSF10AT", 
    "10A. 5-2-8-6-9-1-7-4-8-3-9 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5",
       "6",
       "7",
       "8",
       "9",
       "10",
       "11"
    )
)


#1b

DSF1BP <- checkboxGroupInput(
    "DSF1BP", 
    "1B. 6-3 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)

DSF1BT <- selectInput(
    "DSF1BT", 
    "1B. 6-3 Number Correct", 
    c( "0",
       "1", 
       "2"
    )
)

#2b

DSF2BP <- checkboxGroupInput(
    "DSF2BP", 
    "2B. 6-9-4 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF2BT <- selectInput(
    "DSF2BT", 
    "2B. 6-9-4 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3"
    )
)


#3b
DSF3BP <- checkboxGroupInput(
    "DSF3BP", 
    "3B. 7-2-8-6 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)

DSF3BT <- selectInput(
    "DSF3BT", 
    "3B. 7-2-8-6 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4"
    )
)

#4b

DSF4BP <- checkboxGroupInput(
    "DSF4BP", 
    "4B. 7-5-8-3-6 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF4BT <- selectInput(
    "DSF4BT", 
    "4B. 7-5-8-3-6 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5"
    )
)

#5b

DSF5BP <- checkboxGroupInput(
    "DSF5BP", 
    "5B. 3-9-2-4-8-7 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF5BT <- selectInput(
    "DSF5BT", 
    "5B. 3-9-2-4-8-7 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5",
       "6"
    )
)

#6b


DSF6BP <- checkboxGroupInput(
    "DSF6BP", 
    "6B. 4-1-7-9-3-8-6 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF6BT <- selectInput(
    "DSF6BT", 
    "6B. 4-1-7-9-3-8-6 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5",
       "6",
       "7"
    )
)

#7b
DSF7BP <- checkboxGroupInput(
    "DSF7BP", 
    "7B. 3-8-2-9-5-1-7-4 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF7BT <- selectInput(
    "DSF7BT", 
    "7B. 3-8-2-9-5-1-7-Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5",
       "6",
       "7",
       "8"
    )
)

#8b

DSF8BP <- checkboxGroupInput(
    "DSF8BP", 
    "8B. 7-1-3-9-4-2-5-6-8 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF8BT <- selectInput(
    "DSF8BT", 
    "8B. 7-1-3-9-4-2-5-6-8 4 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5",
       "6",
       "7",
       "8",
       "9"
    )
)

#9b

DSF9BP <- checkboxGroupInput(
    "DSF9BP", 
    "9B. 9-5-2-4-6-8-1-3-7-4 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF9BT <- selectInput(
    "DSF9BT", 
    "9B. 9-5-2-4-6-8-1-3-7-4   Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5",
       "6",
       "7",
       "8",
       "9",
       "10"
    )
)


#10b

DSF10BP <- checkboxGroupInput(
    "DSF10BP", 
    "10B. 2-5-1-9-6-4-8-3-7-1-6 Pass or Fail?",
    c("Pass" = "1",
      "Fail" = "0"
    )
)


DSF10BT <- selectInput(
    "DSF10BT", 
    "10B. 2-5-1-9-6-4-8-3-7-1-6 Number Correct", 
    c( "0",
       "1", 
       "2", 
       "3",
       "4",
       "5",
       "6",
       "7",
       "8",
       "9",
       "10",
       "11"
    )
)



action_demo <- actionButton("clear", "Clear Form")
download_demo <- downloadButton("download", "Download")
file_demo <- fileInput("file_demo", "Upload a PDF", accept = "pdf")
help_demo <- helpText("You can write help text in your form this way")


resetForm <- function(session) {
  updateTextInput(session, "ID", value = "")
  updateDateInput(session, "date_evaluation", value = NA)
  updateSelectInput(session, "DSFMAX", selected=character(0))
  updateCheckboxInput(session, "DSF1AP", value = FALSE)
  updateSelectInput(session, "DSF1AT", selected=character(0))
  updateCheckboxInput(session, "DSF2AP", value = FALSE)
  updateSelectInput(session, "DSF2AT", selected=character(0))
  updateCheckboxInput(session, "DSF3AP", value = FALSE)
  updateSelectInput(session, "DSF3AT", selected=character(0))
  updateCheckboxInput(session, "DSF4AP", value = FALSE)
  updateSelectInput(session, "DSF4AT", selected=character(0))
  updateCheckboxInput(session, "DSF5AP", value = FALSE)
  updateSelectInput(session, "DSF5AT", selected=character(0))
  updateCheckboxInput(session, "DSF6AP", value = FALSE)
  updateSelectInput(session, "DSF6AT", selected=character(0))
  updateCheckboxInput(session, "DSF7AP", value = FALSE)
  updateSelectInput(session, "DSF7AT", selected=character(0))
  updateCheckboxInput(session, "DSF8AP", value = FALSE)
  updateSelectInput(session, "DSF8AT", selected=character(0))
  updateCheckboxInput(session, "DSF9AP", value = FALSE)
  updateSelectInput(session, "DSF9AT", selected=character(0))
  updateCheckboxInput(session, "DSF10AP", value = FALSE)
  updateSelectInput(session, "DSF10AT", selected=character(0))
  updateCheckboxInput(session, "DSF1BP", value = FALSE)
  updateSelectInput(session, "DSF1BT", selected=character(0))
  updateCheckboxInput(session, "DSF2BP", value = FALSE)
  updateSelectInput(session, "DSF2BT", selected=character(0))
  updateCheckboxInput(session, "DSF3BP", value = FALSE)
  updateSelectInput(session, "DSF3BT", selected=character(0))
  updateCheckboxInput(session, "DSF4BP", value = FALSE)
  updateSelectInput(session, "DSF4BT", selected=character(0))
  updateCheckboxInput(session, "DSF5BP", value = FALSE)
  updateSelectInput(session, "DSF5BT", selected=character(0))
  updateCheckboxInput(session, "DSF6BP", value = FALSE)
  updateSelectInput(session, "DSF6BT", selected=character(0))
  updateCheckboxInput(session, "DSF7BP", value = FALSE)
  updateSelectInput(session, "DSF7BT", selected=character(0))
  updateCheckboxInput(session, "DSF8BP", value = FALSE)
  updateSelectInput(session, "DSF8BT", selected=character(0))
  updateCheckboxInput(session, "DSF9BP", value = FALSE)
  updateSelectInput(session, "DSF9BT", selected=character(0))
  updateCheckboxInput(session, "DSF10BP", value = FALSE)
  updateSelectInput(session, "DSF10BT", selected=character(0))
}

# Set up questionnaire interface ----
ui <- fluidPage(
    title = "OHS Neuropsych Data Entry",
    # CSS ----
    # stop the default input containers being 300px, which is ugly
    tags$head(
        tags$style(HTML("
                    .shiny-input-container:not(.shiny-input-container-inline) {
                      width: 100%;
                      max-width: 100%;
                    }
                    "))
    ),
    
    # App title ----
    h3("Digit Span Forward"),
    
    p("Please fill in all values"),
    
    fluidRow(
        column(width=8, ID, date_evaluation),
        column(width=6, DSFMAX)
    ),
    
    fluidRow(
        column(width=6,
               DSF1AP,
               DSF1AT  ),
        column(width=6, 
               DSF1BP,
               DSF1BT
        )),
    fluidRow(
        column(width=6,
               DSF2AP,
               DSF2AT  ),
        column(width=6, 
               DSF2BP,
               DSF2BT
        )), 
    fluidRow(
        column(width=6,
               DSF3AP,
               DSF3AT  ),
        column(width=6, 
               DSF3BP,
               DSF3BT
        )), 
    fluidRow(
        column(width=6,
               DSF4AP,
               DSF4AT  ),
        column(width=6, 
               DSF4BP,
               DSF4BT
        )), 
    fluidRow(
        column(width=6,
               DSF5AP,
               DSF5AT  ),
        column(width=6, 
               DSF5BP,
               DSF5BT
        )), 
    fluidRow(
        column(width=6,
               DSF6AP,
               DSF6AT  ),
        column(width=6, 
               DSF6BP,
               DSF6BT
        )), 
    fluidRow(
        column(width=6,
               DSF7AP,
               DSF7AT  ),
        column(width=6, 
               DSF7BP,
               DSF7BT
        )), 
    fluidRow(
        column(width=6,
               DSF8AP,
               DSF8AT  ),
        column(width=6, 
               DSF8BP,
               DSF8BT
        )), 
    fluidRow(
        column(width=6,
               DSF9AP,
               DSF9AT  ),
        column(width=6, 
               DSF9BP,
               DSF9BT
        )), 
    fluidRow(
        column(width=6,
               DSF10AP,
               DSF10AT  ),
        column(width=6, 
               DSF10BP,
               DSF10BT
        )),
 
    
    actionButton("submit", "Submit"),
    action_demo
)

# Reactive functions ----
server = function(input, output, session) {
    
    # When the Submit button is clicked, save the form data
    observeEvent(input$submit, {
        saveData(input)
        resetForm(session)
        
        # thank the user
        n_responses <- length(list.files(outputDir))
        response <- paste0("Thank you for completing the survey! You are respondant ",
                           n_responses, ".")
        showNotification(response, duration = 0, type = "message")
    })
    
    # clear the fields
    observeEvent(input$clear, {
        resetForm(session)
    })
}

shinyApp(ui, server)


Read more here: https://stackoverflow.com/questions/66267411/resetting-multiple-checkboxes-in-shiny

Content Attribution

This content was originally published by avery_20 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: