使用 googlesheets4 通过 R 中的 shiny 连接到 googlesheets
Connect to googlesheets via shiny in R with googlesheets4
我正在尝试使用此 example 的更新版本通过 shiny 连接到私人 googlesheet,并将此应用程序部署到 shinyapps.io服务器。用户不需要对 google 帐户进行身份验证,因为应用程序使用指定的预先存在的 googlesheet.
我已经按照这个 example(部分复制在这里),试图将令牌保存到我闪亮的应用程序中:
# previous googlesheets package version:
shiny_token <- gs_auth() # authenticate w/ your desired Google identity here
saveRDS(shiny_token, "shiny_app_token.rds")
但尝试将其更新为 googlesheets4,如下所示:
ss <- gs4_get("MY GOOGLE DOC URL") # do the authentication once, manually.
ss
gs4_has_token() # check that the token exists
# get token
ss_token <- gs4_token()
# save the token
save(ss_token, file = "APP PATH ... /data/tk.rdata")
然后在应用程序中,我将此代码放在 shinyApp()
函数之外。
load("data/tk.rdata")
googlesheets4::gs4_auth(token = ss_token, use_oob = T)
在该应用中,我使用从
ss$spreadsheet_id
以上。该应用程序在本地运行。
尝试将应用程序部署到服务器后,出现错误“...无法获取 google 凭据。你 运行 googlesheet s4 在非交互式会话中?...等”我认为令牌将包含足够的信息。
如果有人能给我指出设置指南,并评论这种方法(在 shinyapps.io 上保存令牌)是否安全,我将不胜感激?
我看过其他示例,但似乎大多数都是针对 googlesheets
的先前版本
只需按照 this link 中的说明进行操作即可:
# designate project-specific cache
options(gargle_oauth_cache = ".secrets")
# check the value of the option, if you like
gargle::gargle_oauth_cache()
# trigger auth on purpose to store a token in the specified cache
# a broswer will be opened
googlesheets4::sheets_auth()
# see your token file in the cache, if you like
list.files(".secrets/")
# sheets reauth with specified token and email address
sheets_auth(
cache = ".secrets",
email = "youremail"
)
2021 年 7 月 21 日谷歌sheets4 deprecated some of its function when releasing v1.0.0。
我已更新 以使用 googlesheets4 v1.0.0。
它在部署到 shinyapps.io.
时也有效
设置非交互式身份验证
library(googlesheets4)
# Set authentication token to be stored in a folder called `.secrets`
options(gargle_oauth_cache = ".secrets")
# Authenticate manually
gs4_auth()
# If successful, the previous step stores a token file.
# Check that a file has been created with:
list.files(".secrets/")
# Check that the non-interactive authentication works by first deauthorizing:
gs4_deauth()
# Authenticate using token. If no browser opens, the authentication works.
gs4_auth(cache = ".secrets", email = "your@email.com")
示例 - 将数据添加到 Gooogle Sheet
在 Google Sheets 上创建 Google Sheet 并复制 sheet 的 url。
library(googlesheets4)
gs4_auth(cache=".secrets", email="your@email.com")
ss <- gs4_get("https://docs.google.com/path/to/your/sheet")
sheet_append(ss, data.frame(time=Sys.time()))
如果将您的应用程序部署到 shinyapps.io,请确保将文件部署到 .secrets
文件夹中。
我在这里发帖是因为我在这段旅程中是从这个帖子开始的,我想分享经过数小时的尝试、阅读 gargle、googledrive 和 googlesheets4 文档以及其他许多关于此的帖子后终于奏效的东西问题。
- 我首先使用googlesheets4 方法gs4_auth() 获取凭证并将其存储在
.secrets
文件夹中。如本线程和 here 中所述。这在我的桌面上有效,我很兴奋。它不适用于 shinyapps.io 或我在 AWS EC2 实例上拥有的 shiny-server 的 Ubuntu 18.4 实例。错误是这样的:
“...错误:无法在 non-interactive 会话中获取 Google credentials.Are 你 运行 googledrive?考虑:drive_deauth()
防止尝试获取凭据。直接调用 drive_auth()
并提供所有必要的细节。“
然后我尝试了一种从 here 开始并带我到 这里 的方法
这在 shinyapps.io 上确实有效,但在我的 Ubuntu 闪亮服务器上仍然无效。
这行得通: 我采用 Google 描述的服务帐户方法 here and created a project, then a service account for the project, added Google Sheets API to the project, then downloaded a key as a JSON file. I then used at the top of my app_server.R file googlesheets4::gs4_auth(path = './<path to hidden JSON file folder I called .token>/.token/<JSON key file>.json')
. This still did not work until the final step that is not clearly explained almost anywhere I looked which is to go to the Google sheet in question, and "share" it with the client_email
email address from the JSON key file, giving it editor permissions, in my case. This was finally well explained in this random article: https://robocorp.com/docs/development-guide/google-sheets/interacting-with-google-sheets
终于从我的 AWS 服务器实例上的闪亮服务器读取和写入我的应用程序。我真的希望有人觉得这很有用。
我正在尝试使用此 example 的更新版本通过 shiny 连接到私人 googlesheet,并将此应用程序部署到 shinyapps.io服务器。用户不需要对 google 帐户进行身份验证,因为应用程序使用指定的预先存在的 googlesheet.
我已经按照这个 example(部分复制在这里),试图将令牌保存到我闪亮的应用程序中:
# previous googlesheets package version:
shiny_token <- gs_auth() # authenticate w/ your desired Google identity here
saveRDS(shiny_token, "shiny_app_token.rds")
但尝试将其更新为 googlesheets4,如下所示:
ss <- gs4_get("MY GOOGLE DOC URL") # do the authentication once, manually.
ss
gs4_has_token() # check that the token exists
# get token
ss_token <- gs4_token()
# save the token
save(ss_token, file = "APP PATH ... /data/tk.rdata")
然后在应用程序中,我将此代码放在 shinyApp()
函数之外。
load("data/tk.rdata")
googlesheets4::gs4_auth(token = ss_token, use_oob = T)
在该应用中,我使用从
ss$spreadsheet_id
以上。该应用程序在本地运行。
尝试将应用程序部署到服务器后,出现错误“...无法获取 google 凭据。你 运行 googlesheet s4 在非交互式会话中?...等”我认为令牌将包含足够的信息。
如果有人能给我指出设置指南,并评论这种方法(在 shinyapps.io 上保存令牌)是否安全,我将不胜感激?
我看过其他示例,但似乎大多数都是针对 googlesheets
只需按照 this link 中的说明进行操作即可:
# designate project-specific cache
options(gargle_oauth_cache = ".secrets")
# check the value of the option, if you like
gargle::gargle_oauth_cache()
# trigger auth on purpose to store a token in the specified cache
# a broswer will be opened
googlesheets4::sheets_auth()
# see your token file in the cache, if you like
list.files(".secrets/")
# sheets reauth with specified token and email address
sheets_auth(
cache = ".secrets",
email = "youremail"
)
2021 年 7 月 21 日谷歌sheets4 deprecated some of its function when releasing v1.0.0。
我已更新
它在部署到 shinyapps.io.
设置非交互式身份验证
library(googlesheets4)
# Set authentication token to be stored in a folder called `.secrets`
options(gargle_oauth_cache = ".secrets")
# Authenticate manually
gs4_auth()
# If successful, the previous step stores a token file.
# Check that a file has been created with:
list.files(".secrets/")
# Check that the non-interactive authentication works by first deauthorizing:
gs4_deauth()
# Authenticate using token. If no browser opens, the authentication works.
gs4_auth(cache = ".secrets", email = "your@email.com")
示例 - 将数据添加到 Gooogle Sheet
在 Google Sheets 上创建 Google Sheet 并复制 sheet 的 url。
library(googlesheets4)
gs4_auth(cache=".secrets", email="your@email.com")
ss <- gs4_get("https://docs.google.com/path/to/your/sheet")
sheet_append(ss, data.frame(time=Sys.time()))
如果将您的应用程序部署到 shinyapps.io,请确保将文件部署到 .secrets
文件夹中。
我在这里发帖是因为我在这段旅程中是从这个帖子开始的,我想分享经过数小时的尝试、阅读 gargle、googledrive 和 googlesheets4 文档以及其他许多关于此的帖子后终于奏效的东西问题。
- 我首先使用googlesheets4 方法gs4_auth() 获取凭证并将其存储在
.secrets
文件夹中。如本线程和 here 中所述。这在我的桌面上有效,我很兴奋。它不适用于 shinyapps.io 或我在 AWS EC2 实例上拥有的 shiny-server 的 Ubuntu 18.4 实例。错误是这样的:
“...错误:无法在 non-interactive 会话中获取 Google credentials.Are 你 运行 googledrive?考虑:drive_deauth()
防止尝试获取凭据。直接调用 drive_auth()
并提供所有必要的细节。“
然后我尝试了一种从 here 开始并带我到 这里 的方法 这在 shinyapps.io 上确实有效,但在我的 Ubuntu 闪亮服务器上仍然无效。
这行得通: 我采用 Google 描述的服务帐户方法 here and created a project, then a service account for the project, added Google Sheets API to the project, then downloaded a key as a JSON file. I then used at the top of my app_server.R file
googlesheets4::gs4_auth(path = './<path to hidden JSON file folder I called .token>/.token/<JSON key file>.json')
. This still did not work until the final step that is not clearly explained almost anywhere I looked which is to go to the Google sheet in question, and "share" it with theclient_email
email address from the JSON key file, giving it editor permissions, in my case. This was finally well explained in this random article: https://robocorp.com/docs/development-guide/google-sheets/interacting-with-google-sheets
终于从我的 AWS 服务器实例上的闪亮服务器读取和写入我的应用程序。我真的希望有人觉得这很有用。