Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub package versioning #451

Open
cristinamullin opened this issue Apr 30, 2024 · 0 comments
Open

GitHub package versioning #451

cristinamullin opened this issue Apr 30, 2024 · 0 comments
Assignees
Labels
Future Improvement Minimum viable function complete, issue includes potential future improvements Helpful User Feedback More Research Needed Package Management

Comments

@cristinamullin
Copy link
Collaborator

Is your feature request related to a problem? Please describe.

Users would like to be able to check what version of the R package they are using, and if they already have the most recent version installed.

Describe the solution you'd like

Add the version to the start up message when the package loads via library(TADA). See example code below.

See: https://stackoverflow.com/questions/67986577/how-to-create-custom-start-up-messages-for-r-packages

https://stackoverflow.com/questions/2192360/library-package-development-message-when-loading

.onAttach <- function(libname, pkgname) {
  packageStartupMessage("This is version ", packageVersion(pkgname), 
                        " of ", pkgname)
}

Then, use “packageVersion” to get current version and “available.packages” to get CRAN version.

Probably need to use tryCatch to avoid timeout issues (or when user does not have internet).

get_cran_version <- function(package_name) {
  cran_version <- tryCatch(available.packages()[package_name, "Version"], error = function(x) NULL)
  return(cran_version)
}

> get_cran_version("baytrends")
[1] "2.0.11"

Here is what ggplot2 has at startup:

https://github.com/tidyverse/ggplot2/blob/main/R/zzz.R

That all assumes CRAN. If working only on GitHub for the package it is more complicated.

Example code below

> library(httr)
> library(jsonlite)
> 
> # Replace 'username' and 'repo' with the GitHub username and repository name
> github_url <- [https://api.github.com/repos/USEPA/TADA/releases/latest](https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapi.github.com%2Frepos%2FUSEPA%2FTADA%2Freleases%2Flatest&data=05%7C02%7CMullin.Cristina%40epa.gov%7C137e90358cd24781390308dc42b8b781%7C88b378b367484867acf976aacbeca6a7%7C0%7C0%7C638458608333286471%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=Zj61D%2B6wXkFMoOMe1muQ2ajYMKcDaBnTa%2F73axjfLDM%3D&reserved=0)
> 
> response <- GET(github_url)
> if (http_status(response)$category == "Success") {
+     latest_release <- content(response)
+     latest_version <- latest_release$tag_name
+     print(paste("Latest version:", latest_version))
+ } else {
+     print("Failed to retrieve latest release information.")
+ }
[1] "Failed to retrieve latest release information."

The kicker here is you have to use “releases” on GitHub. If you do it works.

Here is a package I did for MD DNR.
https://github.com/leppott/MBSStools/releases
And it matches.

> # Replace 'username' and 'repo' with the GitHub username and repository name
> github_url <- [https://api.github.com/repos/leppott/MBSStools/releases/latest](https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapi.github.com%2Frepos%2Fleppott%2FMBSStools%2Freleases%2Flatest&data=05%7C02%7CMullin.Cristina%40epa.gov%7C137e90358cd24781390308dc42b8b781%7C88b378b367484867acf976aacbeca6a7%7C0%7C0%7C638458608333303187%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=G2W2Fk2tASTPs0S6UFcUFUF6fmUCez0CVFyUzby3%2FH8%3D&reserved=0)
> 
> response <- GET(github_url)
> if (http_status(response)$category == "Success") {
+     latest_release <- content(response)
+     latest_version <- latest_release$tag_name
+     print(paste("Latest version:", latest_version))
+ } else {
+     print("Failed to retrieve latest release information.")
+ }
[1] "Latest version: v1.1"

There might be other ways.

Hope this helps,
Erik

@cristinamullin cristinamullin added Future Improvement Minimum viable function complete, issue includes potential future improvements Package Management Helpful User Feedback More Research Needed labels Apr 30, 2024
@cristinamullin cristinamullin changed the title Package versioning GitHub package versioning Apr 30, 2024
@cristinamullin cristinamullin self-assigned this May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Future Improvement Minimum viable function complete, issue includes potential future improvements Helpful User Feedback More Research Needed Package Management
Projects
None yet
Development

No branches or pull requests

1 participant