James Crumpler
James Crumpler

Reputation: 204

shinysurveys results to csv

I really like the package {shinysurveys}; however I cannot determine how to export the individual results from the survey.

Am trying something simple, to export today's date to a csv file, but the csv that is exported does not reflect the survey question or response.

library(tidyverse)
library(shiny)
library(shinysurveys)
library(here)

# tibble ----------------

df_1 <- tibble(
  question = "Date: ISO Standard (Year-Month-Day)",
  option = Sys.Date() %>% as.character(),
  input_type = "text",
  input_id = "project_date",
  dependence = NA,
  dependence_value = NA,
  required = F
)

# ui -----------------

ui <- fluidPage(
  surveyOutput(df = df_1,
               survey_title = "Survey Test",
               survey_description = "This is a test")
)

# server -------------------

server <- function(input, output, session) {
  renderSurvey(df =  df_1)
  
  observeEvent(input$submit, {
    
    file_tibble <- input$submit %>% tibble()
    write_csv(file_tibble, file = here("Summary.csv"))
    
    showModal(modalDialog(
      title = "Congrats, you completed your first shinysurvey!",
      "You can customize what actions happen when a user finishes a survey using input$submit."
    ))
  })
}

# Run ----------------------

shinyApp(ui, server)

The app itself runs as expected, but would like some help with exporting results.

Upvotes: 2

Views: 512

Answers (1)

Jonathan Trattner
Jonathan Trattner

Reputation: 71

Glad you like the package! In case it's still of interest, I added some new features in the latest version of shinysurveys that should help you with this. There's a new function, getSurveyData() that aggregates user responses. I've also added support for custom input extensions so you can actually use shiny's dateInput(). Here's a brief overview of the new features, with links to more in-depth documentation.

A modified version of your app may look like this:

library(shiny)
library(shinysurveys)
library(tibble)

# Extend Input Type -------------------------------------------------------

extendInputType("date", {
  shiny::dateInput(
    inputId = surveyID(),
    value = surveyOptions(),
    label = surveyLabel()
  )
})

# tibble ----------------

df_1 <- tibble(
  question = "Date: ISO Standard (Year-Month-Day)",
  option = as.character(Sys.Date()),
  input_type = "date",
  input_id = "project_date",
  dependence = NA,
  dependence_value = NA,
  required = F
)

# ui -----------------

ui <- fluidPage(
  surveyOutput(df = df_1,
               survey_title = "Survey Test",
               survey_description = "This is a test")
)

# server -------------------

server <- function(input, output, session) {
  
  renderSurvey()
  
  # upon submission, print the data.
  # could also use write.csv
  observeEvent(input$submit, {
    print(getSurveyData())
  })
  
}

# Run ----------------------

shinyApp(ui, server)

Upvotes: 3

Related Questions