jdtrat / shinysurveys

Develop and deploy surveys in Shiny/R.
https://shinysurveys.jdtrat.com/
Other
95 stars 27 forks source link

Data aggregation from shinyapps.io #52

Open DanMungai1 opened 2 years ago

DanMungai1 commented 2 years ago

After deploying my survey and sharing the link with participants to fill up through shinyapps.io, how do i aggregate the data once it is submitted?

Germ1215 commented 1 year ago

source("global.R")

ui <-fluidPage(

Application title

    titlePanel("Lista de preguntas"),

    #Sidebar with reactive inputs
    sidebarLayout(
      sidebarPanel(
        title = "Inputs",
        selectInput("Figuras", "Lista de figuras", choices = c("Participante","Responsable"))
      ),

      # a table of reactive outputs
      #mainPanel(
        mainPanel(
          tabsetPanel(
            tabPanel("Cuestionario",
                     fluidRow(
                                #surveyOutput(df=teaching_r_questions,
                                  surveyOutput(df=Preguntas,
                                  survey_title = "Formulario de encuesta de desempeño y satisfacción", 
                                  survey_description = paste("Encuesta para la figura:","Paricipante"=" "),
                                  theme = "blue"),
                     actionButton("save","Guardar"),
                     ),
                     fluidRow(shinyjs::hidden(div(
                       id = "thankyou_msg",
                       h3("Thanks, your response was submitted successfully!")
                     )))

                     ),
            tabPanel("Tabla de respuestas",
                     DT::dataTableOutput("responses", width = 500), tags$hr()
                     )
                  )
      #)
    )
)

)

Define server logic

server <- function(input, output,session) {

dat<-eventReactive(input$save, { dat <- tibble(as.data.frame( getSurveyData( custom_id = NULL,

include_dependencies = TRUE,

    #dependency_string = " "
  )[,c("question_id","response")]))

data=setNames(dat.frame(t(dat[,-1])), dat[,1])
#print(data)

#write.xlsx(data, "response.xlsx", col.names=TRUE, row.names=FALSE, append=F)

showModal(modalDialog(
  title = "Gracias por completar el cuestionario"
))

}, ignoreInit = TRUE)

create a data frame called responses

saveData <- function(data) { data <- as.data.frame(t(data)) if (exists("responses")) { responses <<- rbind(responses, data) } else { responses <<- data }

}

loadData <- function() { if (exists("responses")) { responses } }

Whenever a field is filled, aggregate all form data

formData is a reactive function

formData <- reactive({ data <- sapply(fields, function(x) input[[x]]) data })

When the Save button is clicked, save the form data

observeEvent(input$save, { saveData(formData()) })

observeEvent(input$save, {

User-experience stuff

shinyjs::disable("save")
shinyjs::show("thankyou_msg")

tryCatch({
  shinyjs::show("thankyou_msg")
})
#write.csv(create_table(),'submitted.csv')
#saveData(create_table())

}, ignoreInit = TRUE, once = TRUE, ignoreNULL = T)

Show the previous responses

(update with current response when save is clicked)

output$responses <- DT::renderDataTable({

input$save

#loadData()

})

output$responses <- DT::renderDataTable({ input$save df<-as.data.frame(loadData()) df$ID <- row.names(df) DT::datatable( data = df, class = "display nowrap compact", # style extensions = 'Buttons', options = list(dom = "Blfrtip", buttons = list("copy", list(extend = "collection", buttons = list(

buttons = c("csv", "excel", "pdf","dbf"),

                                                  list(extend = "csv",charset ="latin1", bom = TRUE,title = "Cuestionario"),
                                                  list(extend = "excel", charset ="latin1", title = "Cuestionario"),
                                                  list(extend = "pdf",charset ="latin1", title = "Cuestionario")),
                                                  text = "Download")
                                              )
                              )
            )

})

}

Run the application

shinyApp(ui = ui, server = server)