Skip to content

Instantly share code, notes, and snippets.

@JosiahParry
Last active May 10, 2024 22:54
Show Gist options
  • Save JosiahParry/90ed29320ed3a6afaed2ede55115af6c to your computer and use it in GitHub Desktop.
Save JosiahParry/90ed29320ed3a6afaed2ede55115af6c to your computer and use it in GitHub Desktop.
Get package installation size
pkg_size_ <- function(pkg) {
# https://stackoverflow.com/questions/70991241/installed-r-packages-size
pkg_dir <- system.file(package = pkg)
if (pkg_dir == "") {
return(fs::as_fs_bytes(NA))
}
res <- system(
paste("du -sh", system.file(package = pkg), "| awk '{print $1}'"),
intern = TRUE
)
fs::as_fs_bytes(res)
}
pkg_size <- function(pkg) {
fs::as_fs_bytes(unlist(lapply(pkg, pkg_size_)))
}
pkg_size_recursive <- function(pkg) {
deps <- c(pkg, unlist(tools::package_dependencies(pkg, recursive = TRUE, which = "Imports")))
size <- pkg_size(deps)
total_size <- sum(size)
data.frame(
pkg = c(deps, "total"),
size = structure(c(size, sum(size)), class = c("fs_bytes", "numeric"))
) |>
dplyr::arrange(-as.numeric(size))
}
@JosiahParry
Copy link
Author

The downside of this is that its not easy to read. That's why I like fs_bytes it gives you the human readable format!

@J-Moravec
Copy link

The downside of this is that its not easy to read. That's why I like fs_bytes it gives you the human readable format!

That is not a replacement for fs_bytes, but for system("du -s") (I don't think you need the "-h" in there anyway if you are wrapping it in fs_bytes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment