Skip to content

Instantly share code, notes, and snippets.

@iangow
Created February 8, 2024 21:55
Show Gist options
  • Save iangow/07eb493a2f701b54b3c8377071ce936e to your computer and use it in GitHub Desktop.
Save iangow/07eb493a2f701b54b3c8377071ce936e to your computer and use it in GitHub Desktop.
Small script to identify needed packages
library(stringr)
library(dplyr)
path = getwd()
file_list <-
tibble(full_path = list.files(path, full.names = TRUE)) |>
filter(str_detect(full_path, "\\.(qmd)$"))
get_libraries <- function(file) {
libs <-
str_match(readLines(file), "library\\(([A-Za-z0-0]*?)[,\\)]") |>
as_tibble(.name_repair = "unique") |>
select(2)
tibble(file, lib = c(libs[!is.na(libs)], "duckdb", "RPostgres", "tidyverse"))
}
libs_df <-
file_list |>
select(full_path) |>
pull() |>
lapply(get_libraries) |>
bind_rows()
tidyverse_packages <-
tibble(lib = tidyverse::tidyverse_packages()) |>
filter(lib != "tidyverse")
temp <-
libs_df |>
select(lib) |>
distinct() |>
anti_join(tidyverse_packages) |>
filter(!lib %in% c("parallel")) |>
arrange(lib) |>
paste(collapse = '", "')
temp <-
formatR::tidy_source(text = paste0('install.packages(', temp, ')'),
wrap = TRUE, width.cutoff = 60, output = FALSE)
cat(paste0("```r\n", temp$text.tidy , "\n```"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment