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

Dfo update final #3

Merged
merged 6 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ Description: Provides functions for learning about your R libraries, and the
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
URL: https://github.com/ateucher/libminer, http://andyteucher.ca/libminer/
BugReports: https://github.com/ateucher/libminer/issues
Depends: R (>= 4.1)
Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0)
Config/testthat/edition: 3
Imports:
cli,
dplyr,
fs,
purrr
purrr,
rlang
VignetteBuilder: knitr
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(lib)
export(lib_summary)
importFrom(purrr,map_dbl)
importFrom(rlang,.data)
54 changes: 43 additions & 11 deletions R/lib-summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#'
#' Provides a brief summary of the package libraries on your machine
#'
#' @param ... Grouping variables for the summary. Zero or more of the column
#' names from [lib()].
#' @param sizes Should the sizes of the libraries be calculated?
#' Logical; default `FALSE`.
#'
Expand All @@ -12,18 +14,48 @@
#' @examples
#' lib_summary()
#' lib_summary(sizes = TRUE)
lib_summary <- function(sizes = FALSE) {
pkgs <- utils::installed.packages()
pkg_tbl <- table(pkgs[, "LibPath"])
pkg_df <- as.data.frame(pkg_tbl, stringsAsFactors = FALSE)
names(pkg_df) <- c("Library", "n_packages")
lib_summary <- function(..., sizes = FALSE) {
if (!is.logical(sizes)) {
cli::cli_abort("You supplied {.val {sizes}} to {.var sizes}. It should be a {.cls logical} value, not {.obj_type_friendly {sizes}}.")
}

if (sizes) {
pkg_df$lib_size <- map_dbl(
pkg_df$Library,
~ sum(fs::file_size(fs::dir_ls(.x, recurse = TRUE))),
lib() |>
calculate_sizes(do_calc = sizes) |>
dplyr::group_by(...) |>
dplyr::summarise(
n = dplyr::n(),
dplyr::across(dplyr::any_of("size"), sum),
.groups = "drop"
)
}
}

#' calculate sizes
#'
#' @param df a data.frame
#' @param do_calc should the sizes be calculated? If `FALSE`, `df` is
#' returned unaltered.
#'
#' @return df with a lib_size column
#' @noRd
calculate_sizes <- function(df, do_calc) {
if (!isTRUE(do_calc)) return(df)

pkg_df
cli::cli_inform(c("i" = "Calculating sizes..."))

df |>
dplyr::mutate(
size = purrr::map_dbl(
fs::path(.data$LibPath, .data$Package),
\(x) sum(fs::file_size(fs::dir_ls(x, recurse = TRUE)))
)
)
}

#' Generate a tible of installed packages
#'
#' @return a tibble of all packages installed on a system
#' @export
lib <- function() {
utils::installed.packages() |>
dplyr::as_tibble()
}
1 change: 1 addition & 0 deletions R/libminer-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

## usethis namespace: start
#' @importFrom purrr map_dbl
#' @importFrom rlang .data
## usethis namespace: end
NULL
25 changes: 19 additions & 6 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,25 @@ devtools::install_github("ateucher/libminer")

## Example usage

To get a count of installed packages in each of your library locations,
optionally with the total sizes, use the `lib_summary()` function:

```{r example}
To get a nicely formatted tibble of your installed packages, use the `lib()`
function:

```{r}
library(libminer)
lib_summary()
# specify `sizes = TRUE` to calculate the total size on disk of your packages
lib_summary(sizes = TRUE)
lib()
```


To get a count of installed packages, grouped by any combination of variables
in the `lib()` data.frame, optionally with the total sizes, use the `lib_summary()` function:

```{r example}
lib_summary(LibPath, License)
```

Specify `sizes = TRUE` to calculate the total size on disk of your packages

```{r}
lib_summary(LibPath, NeedsCompilation, sizes = TRUE)
```
81 changes: 60 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,67 @@ devtools::install_github("ateucher/libminer")

## Example usage

To get a count of installed packages in each of your library locations,
optionally with the total sizes, use the `lib_summary()` function:
To get a nicely formatted tibble of your installed packages, use the
`lib()` function:

``` r
library(libminer)
lib_summary()
#> Library
#> 1 /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library
#> 2 /private/var/folders/_f/n9fw7ctx3fqf2ty9ylw502g80000gn/T/RtmpTxIv1Z/temp_libpath17e8111f3ca5d
#> 3 /Users/andy/Library/R/arm64/4.3/library
#> n_packages
#> 1 29
#> 2 1
#> 3 191
# specify `sizes = TRUE` to calculate the total size on disk of your packages
lib_summary(sizes = TRUE)
#> Library
#> 1 /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library
#> 2 /private/var/folders/_f/n9fw7ctx3fqf2ty9ylw502g80000gn/T/RtmpTxIv1Z/temp_libpath17e8111f3ca5d
#> 3 /Users/andy/Library/R/arm64/4.3/library
#> n_packages lib_size
#> 1 29 71351828
#> 2 1 14402
#> 3 191 1164955163
lib()
#> # A tibble: 406 × 16
#> Package LibPath Version Priority Depends Imports LinkingTo Suggests Enhances
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 libminer /priva… 0.0.0.… <NA> R (>= … "cli,\… <NA> "knitr,… <NA>
#> 2 abind /Users… 1.4-8 <NA> R (>= … "metho… <NA> <NA> <NA>
#> 3 anytime /Users… 0.3.9 <NA> R (>= … "Rcpp … Rcpp (>=… "tinyte… <NA>
#> 4 arrow /Users… 17.0.0… <NA> R (>= … "asser… cpp11 (>… "blob, … <NA>
#> 5 askpass /Users… 1.2.1 <NA> <NA> "sys (… <NA> "testth… <NA>
#> 6 assertt… /Users… 0.2.1 <NA> <NA> "tools" <NA> "testth… <NA>
#> 7 backpor… /Users… 1.5.0 <NA> R (>= … <NA> <NA> <NA> <NA>
#> 8 base64e… /Users… 0.1-3 <NA> R (>= … <NA> <NA> <NA> png
#> 9 bayespl… /Users… 1.11.1 <NA> R (>= … "dplyr… <NA> "ggfort… <NA>
#> 10 bccamtr… /Users… 0.0.0.… <NA> R (>= … "bcmap… <NA> "DBI,\n… <NA>
#> # ℹ 396 more rows
#> # ℹ 7 more variables: License <chr>, License_is_FOSS <chr>,
#> # License_restricts_use <chr>, OS_type <chr>, MD5sum <chr>,
#> # NeedsCompilation <chr>, Built <chr>
```

To get a count of installed packages, grouped by any combination of
variables in the `lib()` data.frame, optionally with the total sizes,
use the `lib_summary()` function:

``` r
lib_summary(LibPath, License)
#> # A tibble: 42 × 3
#> LibPath License n
#> <chr> <chr> <int>
#> 1 /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/l… GPL 1
#> 2 /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/l… GPL (>… 5
#> 3 /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/l… GPL (>… 1
#> 4 /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/l… GPL-2 … 5
#> 5 /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/l… LGPL (… 1
#> 6 /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/l… Part o… 14
#> 7 /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/l… Unlimi… 2
#> 8 /Users/andy/Library/R/arm64/4.4/library Apache… 1
#> 9 /Users/andy/Library/R/arm64/4.4/library Apache… 2
#> 10 /Users/andy/Library/R/arm64/4.4/library Apache… 5
#> # ℹ 32 more rows
```

Specify `sizes = TRUE` to calculate the total size on disk of your
packages

``` r
lib_summary(LibPath, NeedsCompilation, sizes = TRUE)
#> ℹ Calculating sizes...
#> # A tibble: 7 × 4
#> LibPath NeedsCompilation n size
#> <chr> <chr> <int> <dbl>
#> 1 /Library/Frameworks/R.framework/Versions/4.4-ar… no 2 8.81e5
#> 2 /Library/Frameworks/R.framework/Versions/4.4-ar… yes 23 6.14e7
#> 3 /Library/Frameworks/R.framework/Versions/4.4-ar… <NA> 4 4.70e6
#> 4 /Users/andy/Library/R/arm64/4.4/library no 216 3.23e8
#> 5 /Users/andy/Library/R/arm64/4.4/library yes 158 1.84e9
#> 6 /Users/andy/Library/R/arm64/4.4/library <NA> 2 3.70e6
#> 7 /private/var/folders/_f/n9fw7ctx3fqf2ty9ylw502g… <NA> 1 1.79e4
```
14 changes: 14 additions & 0 deletions man/lib.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/lib_summary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/libminer-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/testthat/_snaps/lib-summary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# lib_summary fails appropriately

Code
lib_summary(sizes = "foo")
Condition
Error in `lib_summary()`:
! You supplied "foo" to `sizes`. It should be a <logical> value, not a string.

---

Code
lib_summary(sizes = 12)
Condition
Error in `lib_summary()`:
! You supplied 12 to `sizes`. It should be a <logical> value, not a number.

25 changes: 14 additions & 11 deletions tests/testthat/test-lib-summary.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
test_that("lib_summary returns expected results", {
test_that("lib_summary returns expected results with defaults", {
res <- lib_summary()
expect_s3_class(res, "data.frame")
expect_equal(ncol(res), 2)
expect_equal(names(res), c("Library", "n_packages"))
expect_type(res$Library, "character")
expect_type(res$n_packages, "integer")
expect_equal(ncol(res), 1)
expect_equal(names(res), "n")
expect_type(res$n, "integer")
})

test_that("lib_summary fails appropriately", {
expect_error(lib_summary(sizes = "foo"), "not interpretable as logical")
test_that("lib_summary returns expected results with defaults", {
res <- lib_summary(LibPath, License, sizes = TRUE)
expect_s3_class(res, "data.frame")
expect_equal(ncol(res), 4)
expect_equal(names(res), c("LibPath", "License", "n", "size"))
expect_type(res$n, "integer")
expect_type(res$size, "double")
})

test_that("sizes argument works", {
res <- lib_summary(sizes = TRUE)
expect_equal(names(res), c("Library", "n_packages", "lib_size"))
expect_type(res$lib_size, "double")
test_that("lib_summary fails appropriately", {
expect_snapshot(lib_summary(sizes = "foo"), error = TRUE)
expect_snapshot(lib_summary(sizes = 12), error = TRUE)
})
Loading