将Shiny输出保存到Googlesheets

时间:2017-05-19 20:55:31

标签: r shiny shinydashboard r-googlesheets

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

我还为Googlesheet

创建了一个单独的Google帐户

登录Google帐户:

用户名:public.thurston@gmail.com
密码:notapassword123
Google表格: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

1 个答案:

答案 0 :(得分:1)

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

而不是input = input$number