Susan Switzer
Susan Switzer

Reputation: 1922

Use Shiny to collect data from users in Google Sheet

I would like to use a Shiny interface to collect data from user inputs, such as in this Medium Article

The article is written for the googlesheets package, but we now need to use googlesheets4.

I think my code will not work due to may lay of understanding of reactive elements.

#load libraries
library(shiny)
library(shinydashboard)
library(googlesheets4)
library(DT)

ui <- fluidPage(

    # Define UI
    ui <- fluidPage(
        # App title ----
        titlePanel("Seflie Feedback"),
        # Sidebar layout with input and output definitions ----
        sidebarLayout(
            # Sidebar to demonstrate various slider options ----
            sidebarPanel(
                # Input: Overall Rating
                sliderInput(inputId = "helpful", 
                            label = "I think this app is helpful",
                            min = 1, 
                            max = 7,
                            value = 3),
                actionButton("submit", "Submit")
            ),
            mainPanel(
            ))
    )
)
server <- function(input, output, session) {
    # Reactive expression to create data frame of all input values ----
    sliderValues <- reactive({

        usefulRating <- input$helpful

        Data <-  data.frame(
            Value = as.character(usefulRating),
            stringsAsFactors = FALSE)
    })

    #This will add the new row at the bottom of the dataset in Google Sheets.
    observeEvent(input$submit, { 
         MySheet <- gs4_find() #Obtain the id for the target Sheet
        MySheet <-   gs4_get('https://docs.google.com/spreadsheets/d/162KTHgd3GngqjTm7Ya9AYz4_r3cyntDc7AtfhPCNHVE/edit?usp=sharing')
        sheet_append(MySheet , data = Data)
    })
}
shinyApp(ui = ui, server = server)

I replaced the gs4_get() with the link rather than the ID to support you in helping me. If you are not able to access the link, you can replace the link with a google sheet ID from your own sheets temporarily.

When I run the code, I see the following: Warning: Error in is.data.frame: object 'Data' not found. When I replace the usefulRating <- input$helpful with usefulRating <- 4 or usefulRating <- 5 or some other value, the data writes to the Sheet.

Thanks for any insights :)

Upvotes: 2

Views: 1666

Answers (1)

Susan Switzer
Susan Switzer

Reputation: 1922

#load libraries
library(shiny)
library(shinydashboard)
library(googlesheets4)
library(DT)

    ui <- fluidPage(
        titlePanel("Seflie Feedback"),
        sidebarLayout(
            sidebarPanel(
                #This is where a user could type feedback
                textInput("feedback", "Plesae submit your feedback"),
           ),
                #This for a user to submit the feeback they have typed
                 actionButton("submit", "Submit")),
            mainPanel())

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

         as.data.frame(input$feedback)

        })
    
    observeEvent(input$submit, {
        Selfie <-   gs4_get('https://docs.google.com/spreadsheets/d/162KTHgd3GngqjTm7Ya9AYz4_r3cyntDc7AtfhPCNHVE/edit?usp=sharing')
        sheet_append(Selfie, data = textB())
    })
}
shinyApp(ui = ui, server = server)

Upvotes: 3

Related Questions