0

我想使用 Shiny 界面从用户输入中收集数据,例如在这篇Medium 文章中

本文是为 googlesheets 包编写的,但我们现在需要使用 googlesheets4。

我认为我的代码将无法工作,因为可能对反应元素有理解。

#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)

我用链接而不是 ID 替换了 gs4_get() 以支持您帮助我。如果您无法访问该链接,您可以暂时将链接替换为您自己工作表中的 Google 工作表 ID。

当我运行代码时,我看到以下内容:警告:is.data.frame 中的错误:找不到对象“数据”。当我将有用Rating <- input$helpful 替换为有用Rating <- 4 或有用Rating <- 5 或其他值时,数据将写入工作表。

感谢您的任何见解:)

4

1 回答 1

1
#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)
于 2020-06-22T14:30:57.437 回答