# Generated by DropToApp library(shiny) library(bslib) ui <- page_fillable( title = "Feedback form", layout_columns( col_widths = c(5, 7), card( card_header("Tell us what you think"), textInput("name", "Name"), textInput("email", "Email", placeholder = "you@example.com"), selectInput( "topic", "Topic", choices = c("Product", "Support", "Billing", "Other") ), conditionalPanel( "input.topic == 'Other'", textInput("topic_other", "Please specify") ), radioButtons( "rating", "How satisfied are you? (1 to 5)", choices = c("1", "2", "3", "4", "5"), selected = "4", inline = TRUE ), textAreaInput("comments", "Comments", rows = 4), checkboxInput("contact", "You may contact me about my feedback"), actionButton("submit", "Send feedback", class = "btn-primary") ), card( card_header("Responses"), tableOutput("responses"), downloadButton("download", "Download as CSV") ) ) ) server <- function(input, output, session) { # Inputs: input$name, input$email, input$topic, input$topic_other, input$rating, input$comments, input$contact # Responses are kept in memory while the app runs responses <- reactiveVal(data.frame( time = character(), name = character(), email = character(), topic = character(), rating = integer(), comments = character(), contact = logical() )) observeEvent(input$submit, { if (!nzchar(trimws(input$name))) { showNotification("Please enter your name.", type = "warning") } else { topic <- if (input$topic == "Other") input$topic_other else input$topic row <- data.frame( time = format(Sys.time(), "%Y-%m-%d %H:%M"), name = input$name, email = input$email, topic = topic, rating = as.integer(input$rating), comments = input$comments, contact = input$contact ) responses(rbind(responses(), row)) updateTextInput(session, "name", value = "") updateTextInput(session, "email", value = "") updateTextAreaInput(session, "comments", value = "") showNotification("Thank you for your feedback!", type = "message") } }) output$responses <- renderTable({ responses() }) output$download <- downloadHandler( filename = "responses.csv", content = function(file) { write.csv(responses(), file, row.names = FALSE) } ) } shinyApp(ui, server)