Skip to content

Instantly share code, notes, and snippets.

@jdtrat
Created May 23, 2022 01:10
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 jdtrat/13432fbff2e5accb1283153a5225451f to your computer and use it in GitHub Desktop.
Save jdtrat/13432fbff2e5accb1283153a5225451f to your computer and use it in GitHub Desktop.
Precursor utilities for the `flo` R package for loading packages/R scripts
load_my_pkgs <- function(pkgs) {
for (i in seq_along(pkgs)) {
library(
pkgs[i], character.only = TRUE,
warn.conflicts = FALSE,
quietly = TRUE
)
}
}
load_my_r <- function(files) {
for (i in seq_along(files)) {
source(files[i], echo = FALSE)
}
}
my_r <- function() {
if (!fs::dir_exists("R")) {
cli::cli_abort(
"Cannot find {.path R/} directory"
)
}
files <- fs::dir_ls("R", glob = "*.R|*.r")
load_my_r(
files = files[files != "R/packages.R"]
)
}
my_pkgs <- function() {
if (fs::file_exists("DESCRIPTION")) {
pkgs <- desc::desc_get_deps(file = "DESCRIPTION")$package
}
if (fs::file_exists("R/packages.R")) {
pkgs <- readLines("R/packages.R")
}
if (!exists("pkgs")) {
cli::cli_abort(
"Cannot find {.path DESCRIPTION} or {.path R/packages.R} file"
)
} else {
load_my_pkgs(pkgs = pkgs)
}
}
my <- function(pkgs = TRUE, r = TRUE) {
if (pkgs) {
my_pkgs()
}
if (r) {
my_r()
}
}
my()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment