2

我正在自动化 R 中的一个流程,该流程以某种方式提取、推送、分析和存储数据,总而言之,在 Googlesheets、Postgresql 和 salesforce 之间处理数据。到目前为止,我已经在我的系统中运行它,但我需要从我的远程登录 ssh 服务器上运行脚本。

问题是,我在 R 中使用“googlesheets”包来验证和阅读谷歌表格,但这需要一个交互式环境来注册/设置。我已经阅读了关于这个问题的其他答案,但他们都建议开设一个谷歌服务帐户。

但是,我不想这样做,因为我只需要阅读那些本身是免费的谷歌表格。我现在没有 GCP、谷歌计算、bigquery 或任何这些,我现在肯定无法获得任何报酬。

有没有办法从非交互环境(linux终端远程登录界面)读取谷歌表格?我愿意尝试其他库或您可能拥有的任何其他 hack。

有什么事吗?

4

2 回答 2

7

我在类似的事情上苦苦挣扎了好几个小时,所以即使这是一个老问题,我认为其他一些人可能会发现这个解决方案很有用。我的问题是通过 Shiny App 以非交互方式读取和修改谷歌表格的身份验证。无论我是否将其保存在应用程序中嵌入的缓存文件夹中,该应用程序都会始终触发舞蹈身份验证过程。

我使用以下指南和问题来指导自己完成整个过程:

尝试以下可重现的示例:

library("googledrive")
library("googlesheets4") # I am using the developing version 0.1.0.9000
library("shiny")

# You want to deploy an app in Shinyapps.io or other server
# FIRST STEP----
# Get the token an store it in a cache folder embedded in your app directory
# designate project-specific cache
options(gargle_oauth_cache = ".secrets")
# options(gargle_quiet = FALSE) # So you can know what is happening
# Authenticate in interactive mode (run the app) ONCE and check if the token 
# has been stored inside the .secrets folder, after that just comment this line
#drive_auth() # Authenticate to produce the token in the cache folder
# Grant permission to googlesheets to access to the token produced
#sheets_auth(token = drive_token())

# SECOND STEP----
# Comment lines 10, 13 and 15 and uncomment lines 21 and 22
# You tell gargle to search the token in the secrets folder and to look
# for an auth given to a certain email (enter your email linked to googledrive!)
drive_auth(cache = ".secrets", email = "enter_your_email@here")
sheets_auth(token = drive_token())

# THIRD STEP---
# Now you can deploy your app in shinyapps.io!!
# Test if your app runs properly in the local version
# Authenticate in ShinyApps.io
# rsconnect::setAccountInfo(name="<ACCOUNT>", token="<TOKEN>", secret="<SECRET>")
# setwd() in your App directory
# library(rsconnect)
# deployApp()
# Enjoy your new App!!

ui <- # Define UI for application that plots random distributions 
  fluidPage(

    # Application title
    titlePanel("Hello Shiny!"),

    # Sidebar with a slider input for number of observations
    sidebarLayout(
      sidebarPanel(
        sliderInput("obs", 
                    "Number of observations:", 
                    min = 1, 
                    max = 1000, 
                    value = 500),
        actionButton(
          "add",
          "Add new entry")
      ),

      # Show a plot of the generated distribution
      mainPanel(
        "Check your googlesheet!!"
      )
    )
  )

server <- function(input, output, session) {
  # Expression that generates a plot of the distribution. The expression
  # is wrapped in a call to renderPlot to indicate that:
  #
  #  1) It is "reactive" and therefore should be automatically 
  #     re-executed when inputs change
  #  2) Its output type is a plot 
  #
  observeEvent(input$add, {
    # You should have or create a googlesheets through google drive with
    # the name "example_shiny"
    wb <- drive_get("example_shiny")
    dt <- read_sheet(wb)
    new_entry <-
      data.frame(ID = tail(dt$ID, 1) + 1, NAME = "new",
                 OBSERVATION = input$obs)
    sheets_append(new_entry, wb)
  })
}

shinyApp(ui, server)
于 2020-01-25T14:18:19.970 回答
1

如果我理解您的问题,我认为 Rstudio 的这一点文档显示了如何做到这一点:https: //support.rstudio.com/hc/en-us/articles/217952868-Generating-OAuth-tokens-from-a-服务器

于 2018-02-14T23:17:36.137 回答