Skip to content

Commit 260d5c8

Browse files
committed
Add r_repos func
1 parent 3f8328c commit 260d5c8

File tree

8 files changed

+189
-2
lines changed

8 files changed

+189
-2
lines changed

DESCRIPTION

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ Suggests:
4646
badger,
4747
hexSticker,
4848
httr,
49-
rvest
50-
RoxygenNote: 7.2.2
49+
rvest,
50+
BiocManager,
51+
githubinstall,
52+
UpSetR,
53+
grDevices
54+
RoxygenNote: 7.2.3
5155
VignetteBuilder: knitr
5256
License: GPL-3
5357
Config/testthat/edition: 3

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export(github_pages_files)
1414
export(github_pages_vignettes)
1515
export(github_permissions)
1616
export(github_traffic)
17+
export(r_repos)
1718
export(readme_header)
1819
importFrom(data.table,":=")
1920
importFrom(data.table,.SD)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `github_metadata`
88
- `github_commits`
99
- `github_traffic`
10+
- `r_repos`
1011

1112
# echogithub 0.99.0
1213

R/r_repos.R

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#' R repositories
2+
#'
3+
#' Report on which repositories R packages are distributed through
4+
#' (e.g. CRAN, Bioc, rOpenSci, and/or GitHub).
5+
#' @param save_path Path to save upset plot to.
6+
#' @param verbose Print messages.
7+
#' @returns Named list.
8+
#'
9+
#' @export
10+
#' @importFrom data.table data.table
11+
#' @examples
12+
#' report <- r_repos()
13+
r_repos <- function(save_path=tempfile(pattern = "upsetr.pdf"),
14+
verbose=TRUE){
15+
16+
requireNamespace("UpSetR")
17+
total <- percent_all <- percent_exclusive <-
18+
count_exclusive <- count_all <- count_exclusive <- NULL;
19+
20+
#### Gather data ####
21+
pkgs <- r_repos_data(verbose = verbose)
22+
#### Upset plot ####
23+
upset <- r_repos_upset(pkgs = pkgs,
24+
save_path = save_path,
25+
verbose = verbose,
26+
sets.bar.color = "slategrey",
27+
main.bar.color = "slategrey",
28+
text.scale = 1.5,
29+
queries = list(list(query = UpSetR::intersects,
30+
params = list("GitHub"),
31+
color = "darkred",
32+
active = TRUE))
33+
)
34+
upset_data <- upset$data
35+
#### Compute percentages ####
36+
stats_1repo <- colSums(upset_data[rowSums(upset_data)==1,])
37+
repo_stats <- data.table::data.table(
38+
repo=names(stats_1repo),
39+
total=length(unique(pkgs$package)),
40+
count_all=colSums(upset_data),
41+
count_exclusive=stats_1repo
42+
)[,percent_all:=(
43+
count_all/total*100)][,percent_exclusive:=(
44+
count_exclusive/total*100)]
45+
#### Return ####
46+
return(list(pkgs=pkgs,
47+
repo_stats=repo_stats,
48+
upset=upset))
49+
}

R/r_repos_data.R

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
r_repos_data <- function(verbose=TRUE){
2+
requireNamespace("rvest")
3+
requireNamespace("BiocManager")
4+
requireNamespace("githubinstall")
5+
# Get all packages per repo
6+
7+
messager("Gathering R package repository data.",v=verbose)
8+
#### Base R ####
9+
baser <- rownames(installed.packages(priority="base"))
10+
11+
#### CRAN ####
12+
cran <- utils::available.packages(
13+
contriburl = "https://cran.rstudio.com/src/contrib") |> data.frame()
14+
15+
#### Bioc ####
16+
# *Note*: This only retrieves Bioc packages in the currently installed
17+
# release of Bioconductor. Packages that are only in older versions of Bioc
18+
# (and were later deprecated) will not be listed here.
19+
### This function gives all packages, including CRAN, Bioc,
20+
# and anything currently installed.
21+
# bioc <- BiocManager::available()
22+
### This only gives Bioc packages ####
23+
repos <- BiocManager::repositories()
24+
repos <- repos[names(repos)!="CRAN"]
25+
bioc <- utils::available.packages(repos = repos) |> data.frame()
26+
27+
#### rOpenSci ####
28+
ropensci <-
29+
rvest::read_html("https://docs.ropensci.org/") |>
30+
rvest::html_element("#repolist") |>
31+
rvest::html_children() |>
32+
rvest::html_text()
33+
34+
## GitHub
35+
36+
# > The githubinstall package uses Gepuro Task Views for getting the list of R packages on GitHub. Gepuro Task Views is crawling the GitHub and updates information every day. The package downloads the list of R packages from Gepuro Task Views each time it was loaded. Thus, you can always use the newest list of packages on a new R session.
37+
# However, you may use an R session for a long time. In such case, gh_update_package_list() is useful.
38+
# gh_update_package_list() updates the downloaded list of the R packages explicitly.
39+
#
40+
# However this is not actually true,
41+
# as the file has not been updated since February 3rd 2018:
42+
# https://github.com/hoxo-m/githubinstall/issues/41
43+
#
44+
# A Pull Request was made in 2019 but it was never integrated:
45+
# https://github.com/pabter/gepuro-task-views-copy/tree/76b7c4e48a704927432f328c6f898cbac0c5731c
46+
47+
githubinstall::gh_update_package_list()
48+
github <- githubinstall::gh_list_packages()
49+
50+
#### Merge all repos ####
51+
pkgs <- rbind(
52+
cbind(package=baser,
53+
repo="base"),
54+
cbind(package=cran$Package,
55+
repo="CRAN"),
56+
cbind(package=bioc$Package,
57+
repo="Bioc"),
58+
cbind(package=ropensci,
59+
repo="rOpenSci"),
60+
cbind(package=github$package_name,
61+
repo="GitHub")
62+
) |>
63+
data.table::data.table()
64+
return(pkgs)
65+
}

R/r_repos_upset.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
r_repos_upset <- function(pkgs,
2+
save_path=tempfile(pattern = "upsetr.pdf"),
3+
verbose = TRUE,
4+
...){
5+
requireNamespace("UpSetR")
6+
repo <- NULL;
7+
8+
messager("Generating upset plot.",v=verbose)
9+
upsetr_data <- lapply(stats::setNames(unique(pkgs$repo),
10+
unique(pkgs$repo)),
11+
function(r){
12+
unique(subset(pkgs,repo==r)$package)
13+
}) |> UpSetR::fromList()
14+
#### Plot ####
15+
upset_plot <- UpSetR::upset(data = upsetr_data,
16+
...)
17+
#### Save ####
18+
if(!is.null(save_path)){
19+
requireNamespace("grDevices")
20+
## PDF
21+
if(endsWith(save_path,".png")){
22+
## PNG
23+
grDevices::png(save_path)
24+
upset_plot
25+
grDevices::dev.off()
26+
} else {
27+
grDevices::pdf(save_path)
28+
upset_plot
29+
grDevices::dev.off()
30+
}
31+
}
32+
#### Return ####
33+
return(list(data=upsetr_data,
34+
plot=upset_plot))
35+
}

man/r_repos.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-r_repos.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test_that("r_repos works", {
2+
3+
report <- r_repos()
4+
repos <- c("base","CRAN","Bioc","rOpenSci","GitHub")
5+
testthat::expect_gte(nrow(report$pkgs),50000)
6+
testthat::expect_true(all(repos %in% unique(report$pkgs$repo)))
7+
testthat::expect_true(all(repos %in% report$repo_stats$repo))
8+
testthat::expect_true(methods::is(report$upset$plot,"upset"))
9+
})

0 commit comments

Comments
 (0)