【问题标题】:Is it possible to find the latest GitHub release of a repo with R?是否可以使用 R 找到最新的 GitHub 版本的存储库?
【发布时间】:2021-11-11 16:53:16
【问题描述】:

我正在寻找一种在 R 中从 GitHub 存储库获取最新版本的方法。例如,使用存储库 acornamr/acorn-dashboard,它将返回字符串 "v2.0.5"https://github.com/acornamr/acorn-dashboard/releases,截至 2021 年 9 月 16 日)。

我曾尝试查看remotes::install_github()https://github.com/r-lib/remotes/blob/master/R/install-github.R的源代码,但没有成功。

【问题讨论】:

    标签: r github


    【解决方案1】:

    使用 gh 包:

    library(gh)
    
    releases <- gh("GET /repos/{owner}/{repo}/releases", 
       owner = "acornamr",
       repo = "acorn-dashboard")
    
    releases[[1]][["tag_name"]]
    # "v2.0.5"
    

    【讨论】:

    • 谢谢!我试图理解为什么上面的命令甚至示例 gh("/users/hadley/repos") 在我的机器上失败,原因如下:Error: GitHub PAT must have one of these forms: * 40 hexadecimal digits (older PATs) * A 'ghp_' prefix followed by 36 to 251 more characters (newer PATs) Running R 4.1 on Mac Intel with gh 1.3.0.
    • @tic-toc-choc 抱歉,不知道。
    【解决方案2】:

    嗯,稍微偏离了 OP 的主题,但相关答案:

    Microsoft 存储了 CRAN 的 daily snapshots。这项免费服务称为 MRAN。

    您可以将 MRAN 每日快照设置为 repos 选项,install.packages() 将在下载时使用该存储库。这样,您始终可以从特定时间点下载软件包。在您的情况下,您总是想要最新的,因此请使用 Sys.Date() 作为您选择的日期。

    # MRAN snapshots
    base_url <- "http://mran.revolutionanalytics.com/snapshot/"
    
    # download from MRAN on this date
    when <- Sys.Date() # or any date as YYYY-MM-DD
    
    # set options
    options(repos = list(CRAN = paste0(base_url, when)))
    
    # the repo option set above defines where to download packages from
    install.packages("my_package")
    

    这在使用 Docker 构建生产级 R 代码时特别有用,因为您可以确保安装到映像中的包始终来自相同的静态日期。我使用这样的东西:

    ARG WHEN
    
    RUN R -e "options(repos = \
      list(CRAN = 'http://mran.revolutionanalytics.com/snapshot/${WHEN}')); \
      install.packages('my_package')"
    

    【讨论】:

      【解决方案3】:

      我最终对网页进行了网页抓取并提取了正确的 html 元素:

      library(rvest)
      library(stringr)
      
      read_html("https://github.com/acornamr/acorn-dashboard/releases/latest") |> 
        html_element(".release-header .f1") |>
        html_text() |>
        str_trim()
      

      返回"v2.0.5"

      与@Stéphane Laurent 解决方案与library(gh) 相比,此方法的优点是您不必处理GitHub PAT。缺点是它依赖于 GitHub.com 的 HTML 元素,这些元素可能会随着网站设计而发展。

      【讨论】:

        【解决方案4】:

        截至目前,我不确定 remotes::install_github 是否可行。您可以通过以下方式选择要下载的版本

        remotes::install_github('acornamr/acorn-dashboard@v2.0.5')
        

        更多阅读请看here

        【讨论】:

          猜你喜欢
          • 2022-01-15
          • 2016-07-10
          • 1970-01-01
          • 2015-09-03
          • 2018-07-05
          • 2022-11-20
          • 2012-06-01
          • 1970-01-01
          • 2019-01-13
          相关资源
          最近更新 更多