简体   繁体   中英

Shiny: How to save inputs from several pages?

Regarding Shiny:

I want several human coders to rate 100 different texts. On each page there should be just a simple drop-down menu with the available gradings. I can implement this for a single page/text with a simple "Submit" button and the answer is saved in a single csv-file.

But now I want to generalize this for all the 100 texts - so that each page has a new text shown (I would have implemented this as a for loop?) and want to save all the 100 answers in one .csv-file.

I am new to Shiny (but not that bad at R) but cannot find a functionality that provides both - multiple pages and multiple saved inputs.

My current code looks like this for a single page (mostly inspired by https://deanattali.com/2015/06/14/mimicking-google-form-shiny ):


humanTime <- function() format(Sys.time(), "%Y%m%d-%H%M%OS")

fieldsMandatory <- c("new_info", "agreement", "rating")

fieldsAll <- c("new_info", "agreement", "rating")
responsesDir <- file.path("responses")
epochTime <- function() {
  as.integer(Sys.time())
}

shinyApp(
  ui = fluidPage(
    shinyjs::useShinyjs(),

    titlePanel("Kommentarbewertungen Spiegel Online - Pre-Test"),
    mainPanel("This is the main panel"), 
    br(),
    code(text[1]),
    br(),
    div(
      id = "form", align="center",
      selectInput("new_info", "Bringt der Kommentar neue Informationen ein?",
                  c("","0 - nein", "1 - ja", "2 - unentschlossen")),
      selectInput("agreement", "Stimmen Sie dem Kommentar zu?",
                  c("","0 - nein", "1 - ja", "2 - unentschlossen")),
      selectInput("rating", "Bewerten Sie die Qualitaet des Kommentars auf einer Skala von 1 bis 5",
                  c("","1 - sehr schlecht", "2 - schlecht", "3 - mittel", "4 - gut", "5 - sehr gut")),
      actionButton("submit", "Submit", class = "btn-primary")
    ),
    br(),
    actionButton("prevBtn", "< Previous"),
    actionButton("nextBtn", "Next >")


  ),
  server = function(input, output, session) {
    observe({
    # check if all mandatory fields have a value
    mandatoryFilled <-
      vapply(fieldsMandatory,
             function(x) {
               !is.null(input[[x]]) && input[[x]] != ""
             },
             logical(1))
    mandatoryFilled <- all(mandatoryFilled)

    # enable/disable the submit button
    shinyjs::toggleState(id = "submit", condition = mandatoryFilled)
  })

    formData <- reactive({
      data <- sapply(fieldsAll, function(x) input[[x]])
      data <- c(data, timestamp = epochTime())
      data <- t(data)
      data
    })

    saveData <- function(data) {
      fileName <- sprintf("%s_%s.csv",
                          humanTime(),
                          digest::digest(data))

      write.csv(x = data, file = file.path(responsesDir, fileName),
                row.names = FALSE, quote = TRUE)
    }

    # action to take when submit button is pressed
    observeEvent(input$submit, {
      saveData(formData())
    })
  }
)

Thanks a lot.

I would use the submit button as an event. As you said upon the pressing of this button the user's inputs are saved to a csv file, why not keep appending the same csv?

 observeEvent(input$SubmitButton, {

 Test<-c(input$rating, input$blah, input$blah)

 write.table(Test, "Test.csv", row.names=F, sep=",",col.names=F,append=T)

 #code to advance to next text
 }

After the user presses submit, automatically advance shiny to the next page, when the user inputs again, and presses submit, the csv will automatically be opened and added too with the new inputs. As long as the order the texts appear is consistent you should have all the answers saved in one csv.

Note: I use write table for .csv so I can easily open in excel. (I hope this helps)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM