将 Shiny 输出保存到 Google 表格

Save Shiny output to Googlesheets

我有一个闪亮的应用程序,它产生的输出我想保存到 Googlesheet。该应用程序根据用户输入生成值,并且我创建了一个工作模型(尽可能缩写)来复制下面代码片段框中所见的错误。

我还为示例创建了一个单独的 Google 帐户 Googlesheet

登录 Google 帐户:

用户名:public.thurston@gmail.com
密码:notapassword123
GoogleSheet: https://docs.google.com/spreadsheets/d/1y7dFTqUX3pLJ_VA-jjaWzMQydOVpwz8brPDMGpDCavk/edit#gid=0

library(shiny)
library(googlesheets)

#Code to download a new API token commented here:
#token = gs_auth(new_user = TRUE)
#saveRDS(token, "googlesheets_token.rds")

#Login to the following Google account:
#Username: public.thurston@gmail.com
#Password: notapassword123
#GoogleSheet: https://docs.google.com/spreadsheets/d/1y7dFTqUX3pLJ_VA-jjaWzMQydOVpwz8brPDMGpDCavk/edit#gid=0

ui <- shinyUI(navbarPage("Example",
  tabPanel("Example",
    sidebarLayout(
      sidebarPanel(
       numericInput("number", label = "I am the number input", value = 10),
       actionButton("saveresult", label = "Refresh & Save Result")        
      ), 
      mainPanel(
        h2(strong("Input value:")),
        h4(textOutput("text_number"))
      )
    )
  )
)
)

server <- shinyServer(function(input, output) {
  values <- reactiveValues()
  observe({
    input$saveresult
    values$number <- input$number
  })
  output$text_number <- renderText({
    input$saveresult
    paste("Number:", isolate(input$number))
  })
  observeEvent(input$saveresult, {
    gs_auth(token = "googlesheets_token.rds")
    gs_add_row(ss = gs_key("1y7dFTqUX3pLJ_VA-jjaWzMQydOVpwz8brPDMGpDCavk"), 
               ws = "Sheet1",
               input = output$text_number
    )
  })
})

shinyApp(ui = ui, server = server)

当我尝试将输出保存到 Googlesheet 时,出现以下错误。

Warning: Error in $.shinyoutput: Reading objects from shinyoutput object not allowed.
Stack trace (innermost first):
    72: $.shinyoutput
    71: $
    70: nrow
    69: gs_add_row
    68: observeEventHandler [#13]
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: <Promise>
ERROR: [on_request_read] connection reset by peer

您无法读取输出值 output$text_number,因此您可以使用 input = values$numberinput = input$number

而不是 input = output$text_number