Skip to content

Instantly share code, notes, and snippets.

@dirkschumacher
Last active September 29, 2020 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirkschumacher/41e8232b1d6f8cb39fbbf0eca522aa81 to your computer and use it in GitHub Desktop.
Save dirkschumacher/41e8232b1d6f8cb39fbbf0eca522aa81 to your computer and use it in GitHub Desktop.
# investigate your project or package dependencies
# using the {tidyverse}, {renv} and {tools}.
# since this code is wrapped in {reprex}, the below library calls
# will be identified as dependent packages although only {tidyverse}, {renv}
# and {tools} are actually used.
library(tidyverse, warn.conflicts = FALSE) # ironically the most deps :)
# add some more dependencies as an example
suppressPackageStartupMessages(library(caret, warn.conflicts = FALSE))
suppressPackageStartupMessages(library(tidymodels, warn.conflicts = FALSE))
suppressPackageStartupMessages(library(survival, warn.conflicts = FALSE))
suppressPackageStartupMessages(library(surveillance, warn.conflicts = FALSE))
suppressPackageStartupMessages(library(ROI.plugin.glpk, warn.conflicts = FALSE))
deps <- renv::dependencies() %>%
pull(Package) %>%
unique() %>%
tools::package_dependencies(recursive = TRUE) %>%
keep(~ length(.x) > 0) %>%
map2(names(.), ~ tibble(pkg = .y, dependency = .x)) %>%
bind_rows()
#> Finding R package dependencies ... Done!
# quick summary
summarise(
deps,
direct_dependencies = n_distinct(pkg),
indirect_dependencies = n_distinct(dependency)
)
#> # A tibble: 1 x 2
#> direct_dependencies indirect_dependencies
#> <int> <int>
#> 1 9 156
# top dependencies
group_by(deps, dependency) %>%
summarise(n = n_distinct(pkg), .groups = "drop") %>%
arrange(desc(n))
#> # A tibble: 156 x 2
#> dependency n
#> <chr> <int>
#> 1 utils 9
#> 2 methods 8
#> 3 stats 8
#> 4 grDevices 6
#> 5 evaluate 5
#> 6 glue 5
#> 7 graphics 5
#> 8 grid 5
#> 9 lattice 5
#> 10 magrittr 5
#> # … with 146 more rows
# test the effect of removing a single package
deps$pkg %>%
unique() %>%
map(function(pkg) {
dependencies_after_removal <- n_distinct(
filter(deps, pkg != !!pkg) %>% pull(dependency)
)
tibble(
removed_pkg = pkg,
indirect_dependencies_after_removal = dependencies_after_removal,
reduction = n_distinct(deps$dependency) - indirect_dependencies_after_removal
)
}) %>%
bind_rows() %>%
arrange(indirect_dependencies_after_removal)
#> # A tibble: 9 x 3
#> removed_pkg indirect_dependencies_after_removal reduction
#> <chr> <int> <int>
#> 1 tidyverse 130 26
#> 2 tidymodels 137 19
#> 3 surveillance 145 11
#> 4 caret 152 4
#> 5 ROI.plugin.glpk 152 4
#> 6 knitr 156 0
#> 7 renv 156 0
#> 8 survival 156 0
#> 9 rmarkdown 156 0
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# For more information, please refer to <http://unlicense.org/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment