Is it possible to find the latest GitHub release of a repo with R?

Viewed 76
4 Answers

With the gh package:

library(gh)

releases <- gh("GET /repos/{owner}/{repo}/releases", 
   owner = "acornamr",
   repo = "acorn-dashboard")

releases[[1]][["tag_name"]]
# "v2.0.5"

Hmm, slightly off topic to the OP, but related answer:

Microsoft stashes daily snapshots of CRAN. This free service is called MRAN.

You can set an MRAN daily snapshot as a repos option and install.packages() will use that repo when downloading. In this way, you can always download packages from a particular point in time. In your case, you always want the latest, so use Sys.Date() as your day to select.

# 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")

This is particularly useful when using Docker to build production-grade R code because you can ensure the packages installed into your image always come from the same, static date. I use something like this:

ARG WHEN

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

I ended up web scraping the page and extracting the right html element:

library(rvest)
library(stringr)

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

returns "v2.0.5"

The Pro of this method compare to @Stéphane Laurent solution with library(gh) is that you don't have to deal with GitHub PAT. The Cons is that it's dependant on HTML elements of GitHub.com that could evolve with the website design.

As of right now, I am not sure that is possible with remotes::install_github. You are able to select which release you would like to download by using the following

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

For further reading, look here

Related