【问题标题】:Downloading file from a private repo into R将文件从私人仓库下载到 R
【发布时间】:2023-03-23 04:18:01
【问题描述】:

我有一个 zip 文件,其中包含私人 Github 存储库中的数据。我正在编写一些 R 代码,我希望将数据下载到 R 中。当我运行以下代码时,我得到一个 HTTP 状态为“404 Not Found”。但是,当 repo 公开时,就不会发生这种情况。

如何修复代码以下载文件?我相信我需要获取访问令牌,但我也不知道如何获取它并将其添加到代码中。如果有人可以提供帮助,我将不胜感激。谢谢。

# We give the url a name
url <- "https://github.com/aquijanoruiz/disability_benefits_EC/raw/master/ENSANUT_datasets/BDD_ENSANUT_2018_STATA_.zip"
# We create a temporary directory
td <- tempdir()
# We create the placeholder file
tf <- tempfile(tmpdir=td, fileext = ".zip")
# We download the data into the placeholder file
download.file(url,tf)

【问题讨论】:

  • 有没有机会让它发挥作用?
  • 无法正常工作。将文件上传到我的谷歌驱动器,不需要令牌,只需 url。感谢您的提问。
  • 好的,谢谢您的反馈。我已将您的解决方法包含在答案中以获得更多可见性。

标签: r github


【解决方案1】:

r 3.6.2 download.file 开始,您至少需要添加一个标头,其中包含Authorization: token xxx,以便正确进行身份验证(因此有权读取存储库内容)。
使用PAT (Personnal Access Token)

还建议添加media type,如“How can I download a single raw file from a private GitHub repo using the command line?”中所示,但在您的情况下可能不需要,因为您直接使用/raw/ URL。

我会使用remote.download,它包装了download.file,并允许轻松设置auth_header

download(".", url, "<yourToken>")

然而,OP Alonso Quijano 不得不求助于解决方法(来自 the comments):

我将文件上传到我的谷歌驱动器,不需要令牌,只需 url。

【讨论】:

  • 谢谢,您能否告诉我应该选择哪些范围或权限才能允许下载文档?
  • @AlonsoQuijano 具有范围 repo (docs.github.com/en/developers/apps/building-oauth-apps/…) 的令牌应该足够了。假设您有权访问该私有存储库(因为您是该私有存储库的所有者,或者是该私有存储库的注册合作者)
  • 再次感谢。我正在尝试按照您的建议使用下载功能。我加载了remotes 包,但它似乎没有找到该功能。我使用了以下代码: library(remotes) download(path = tf, url = url, auth_token = "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") 我还尝试在 download.file 函数中添加标头。 download.file(tf, url, headers = "授权:令牌 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")。我已经留下令牌进行测试。我以后可以消除。
  • @AlonsoQuijano 应该是 dowload.file(path = ...)
  • 嗨,download.file 似乎没有 path 参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-11
  • 2017-07-19
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
相关资源
最近更新 更多