Note that before this will work you will need to run the following in your console (once installed googlesheets() )
1) ttt <- gs_auth() # now follow the html. prompts to login
2) saveRDS(ttt, "ttt.rds") # then copy ttt.rds file to the app's Dir()
3) Now one needs to create a worksheet to use using the following code:
Data <- gs_new("Data") %>%
gs_ws_rename(from = "Sheet1", to = "Data")
4) Insert the titles that we want
Data <- Data %>%
gs_edit_cells(ws = "Data", input = cbind("Score1", "Score2", "Time", "Mean"), trim = TRUE) # Note! you will need to have manually add one row of data to your google sheet otherwise it will error
UI
----------------------------------------------------------------------------------------
library(shiny)
library("googlesheets") # Don't forget to install.packages("googlesheets")
library("DT") # Don't forget to install.packages("DT")
shinyUI(fluidPage( # Open UI
"survey demo", # Title
# Questions
selectInput(inputId = "Score1", # What we are calling the object
label = "The Question 1", # Question
choices = c("Disagree Strongly" = 1, "Disagree" = 2, "Disagree Somewhat " = 3, # Responses
"Neither Agree nor Disagree" = 4, "Agree Somewhat" = 5, "Agree" = 6,
"Agree Strongly" = 7)
),
selectInput(inputId = "Score2", # What we are calling the object
label = "The Question 1", # Question
choices = c("Disagree Strongly" = 1, "Disagree" = 2, "Disagree Somewhat " = 3, # Responses
"Neither Agree nor Disagree" = 4, "Agree Somewhat" = 5, "Agree" = 6,
"Agree Strongly" = 7)
),
radioButtons("radio", label = h3("Do you consent to having your anonymous data stored for research"), # insert Radio for data storage consent
choices = list("Sure" = 1, "No, erase all evidence I was here" = 2
)),
# Submitbutton
actionButton(inputId = "Action", label = "Submit"), tags$hr(), # Create submit action button
dataTableOutput('mytable') # Data display with downloads using DT
)) # Close Shiny UI and fluid page
Server
---------------------------------------------------------------------------------------------------------------
library(shiny)
library("googlesheets") # Don't forget to install.packages("googlesheets")
library("DT") # Don't forget to install.packages("DT")
suppressMessages(library(dplyr)) # Don't forget to install.packages("dplyr")
gs_auth(new_user = FALSE, gs_auth(token = "ttt.rds")) # login from token file "ttt.rds" in dir(). see post for how to do this
shinyServer(function(input, output) { # Open Shiny Server
Results <- reactive(c(Score1, Score2, Sys.Date(), mean(Score1,Score2))) # Create reactive data to input
##### Function 1, Add data when action button is pressed ####
observeEvent(input$Action, { # Observe event action from Actionbutton
Data <- Data %>%
gs_add_row(ws = "Data", input = Results() ) # When actionbutton is pressed this will add their data to the good .doc
})
##### Function 2, Create a data display with download option ####
output$mytable <- renderDataTable({gs_read(Data)}, filter = 'top', extensions = c('Scroller', 'Buttons'), options = list( # use the Scroller and Buttons extensions for scroll options and download options respectively
dom = 'Bfrtip',
buttons =
list('copy', 'print', list(
extend = 'collection',
buttons = c('csv', 'excel', 'pdf'), # Modify the downloads extension to make it nicer (places download options in menu under "Download")
text = 'Download'
) # close list
) # close list
) # close list
, caption = 'Table X: This is a simple caption for the table.') # Adds the caption. Note, I tried to add " %>% formatDate('Time', 'toDateString') " here but does not work
} # Close shiny server function
) # Close shiny server