R pacakge download and managment tool

Tool
R
Author

Tony D

Published

March 14, 2025

A guide to R package management using the pak and cranlogs packages, covering installation, version checking, and analyzing download statistics.

This document provides a comprehensive guide to R package management, focusing on the pak and cranlogs packages. It demonstrates how to use pak for installing, updating, and managing packages from various sources, including CRAN, GitHub, and local files. The guide also covers how to use cranlogs to analyze package download statistics and retrieve information about packages from GitHub. Additionally, it briefly mentions other useful tools for building R packages.

pak installs R packages from CRAN, Bioconductor, GitHub, URLs, git repositories, local files and directories. It is an alternative to install.packages() and devtools::install_github(). pak is fast, safe and convenient.

R packages download and managment tool

install pak

Code
install.packages("pak")

load pak

Code
library(pak)

check pak version

Code
pak_sitrep() 

install pacakge from cran

Code
pkg_install("tibble")

install pacakge from github

Code
pkg_install("tidyverse/tibble")

install pacakge file tar.gz from website

Code
pkg_install(
  "url::https://cran.r-project.org/src/contrib/Archive/tibble/tibble_3.1.7.tar.gz"
)

uninstall package

Code
pkg_remove("tibble")

check package

Code
pkg_deps_tree("tibble")

show all Dependencies

Code
pkg_deps("tibble")

Explain dependencies

Code
pkg_deps_explain("tibble", "rlang")

check pacakge history on cran

Code
pkg_history("tibble")

update package

Code
pkg_install("tibble")

Update all dependencies of a package

Code
pkg_install("tibble", upgrade = TRUE)

Check R pacakge info on Cran

Code
library(pak)
pkg_install("cranlogs")

total pacakge download from last week

Code
library(cranlogs)
cran_downloads(when = "last-week")

total pacakge download from 2014

Code
data=cran_downloads(from = "2014-01-01", to = "2024-12-31")
library(plotly)
plot_ly(data, x = ~date, y = ~count, mode = "lines")

top pacakge download from last week

Code
cran_top_downloads("last-week")

one pacakge download from last week

Code
pacakge_name="tibble"
lastweek=cran_downloads(when = "last-week", package = pacakge_name)
lastweek
Code
print(paste(pacakge_name,"last week been downloaded",sum(lastweek$count),"times"))

check pacakge github info

there is another R package for this gh

Code
library(tidyverse)
library(httr)
library(cranlogs)
library(ggrepel)
library(scales)
library(lubridate)
library(knitr)
library(stringr)
Code
# authenticate to github -------------------------------------------------------
# use Hadley's key and secret
myapp <- oauth_app("github",
                   key = "Ov23lizHQuHjLsuQlgXc",
                   secret = "44ee97da2b3fc85c02bf46b236d1f23739945c8e")
github_token <- oauth2.0_token(oauth_endpoints("github"), myapp)
gtoken <- config(token = github_token)


date_without_zeros <- function(x){
  gsub("0(\\d)", "\\1", format(x, "%m/%d/%y"))
}

gh_from_url <- function(x){
  x <- gsub("http://", "https://", tolower(x))
  x <- gsub("www\\.github\\.com", "github.com", x)
  x <- gsub("^github.com", "https://github.com", x)
  x <- gsub("/issues", "", x)
  x <- gsub("\\.git", "", x)
  x <- gsub("For source code, development versions and issue tracker see", "", x, ignore.case=TRUE)
  x <- trimws(x)
  x <- gsub("development versions and issue tracker see ", "", x, ignore.case=TRUE)
  x <- trimws(x)
  x <- gsub("^<(.*)>$", "\\1", x)
  if(grepl(',', x)){
    x <- strsplit(x, ",")[[1]]
    x <- trimws(x[min(which(grepl('http://github.com|https://github.com|http://www.github.com', x)))])
  }
  if(grepl(' ', x)){  
    x <- strsplit(x, " ")[[1]]
    x <- trimws(x[min(which(grepl('http://github.com|https://github.com|http://www.github.com', x)))])    
  }
  x <- gsub("^(.*)/(.*)#\\([a-zA-z]+\\)\\b", "\\1/\\2", x)
  x <- gsub("^(.*)/(.*)[[:space:]]+\\([a-zA-z]+\\)\\b", "\\1/\\2", x)
  x <- gsub("^(.*) http(.*)$", "http\\2", x)
  x <- trimws(x)
  x <- gsub("/$", "", x)
  x <- trimws(x)
  return(x)
}

aut_maintainer_from_details <- function(x){
  x <- gsub("'|\"", "", x)
  if(grepl(',', x)){
    x <- strsplit(x, "\\],")[[1]]
    aut_cre_ind <- grepl(pattern='\\[aut, cre|\\[cre, aut|\\[cre', x, ignore.case=TRUE)
    if(any(aut_cre_ind)){
      x <- x[min(which(aut_cre_ind))]
      x <- gsub("\\[aut, cre|\\[cre, aut|\\[cre", "", x)
    }
    x <- strsplit(x, ",")[[1]][1]
    x <- trimws(gsub("\\]", "", x))
    x <- trimws(gsub(" \\[aut", "", x))
  }
  x <- trimws(gsub(" \\(.*\\)$", "", x))
  x <- trimws(gsub(" <.*>$", "", x))
  return(x)
}

gh_star_count <- function(url){
  Sys.sleep(0.5)
  stars  <- tryCatch({
    this_url <- gsub("https://github.com/", "https://api.github.com/repos/", url)
    req <- GET(this_url, gtoken)
    stop_for_status(req)
    cont <- content(req)
    cont$stargazers_count  
  }, error = function(e){
    return(NA_integer_)
  })
  return(stars)
}

gh_forks_count <- function(url){
  Sys.sleep(0.5)
  stars  <- tryCatch({
    this_url <- gsub("https://github.com/", "https://api.github.com/repos/", url)
    req <- GET(this_url, gtoken)
    stop_for_status(req)
    cont <- content(req)
    cont$forks  
  }, error = function(e){
    return(NA_integer_)
  })
  return(stars)
}




gh_last_commit_date <- function(url){
  last_commit <- tryCatch({
    this_url <- gsub("https://github.com/", "https://api.github.com/repos/", url)
    req <- GET(paste0(this_url, "/commits?page=1&per_page=1"), gtoken)
    stop_for_status(req)
    cont <- content(req)
    cont[[1]]$commit$committer$date
  }, error = function(e){
    return(NA_character_)
  })
  return(last_commit)
}
Code
github_url="https://github.com/r-lib/styler"
Code
gh_star_count(github_url)
Code
gh_last_commit_date(github_url)
Code
gh_last_commit_date(github_url)
Code
gh_watches_count(github_url)

Tool for build a R pacakge

devtools/roxygen2/usethis/testthat/pkgdown

reference:

https://github.com/r-lib/pak