-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.R
More file actions
41 lines (40 loc) · 1.82 KB
/
utils.R
File metadata and controls
41 lines (40 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
################################################################################
## File: utils.R ##
## Project: analysis-template-R ##
## Created: 2025-11-05 ##
## Author: Dimitri Baptiste ##
## ----- ##
## Last Modified: 2026-01-20 ##
## Modified By: Dimitri Baptiste ##
## ----- ##
## Copyright (c) 2025-2026 Syndemics Lab at Boston Medical Center ##
################################################################################
#' Ensure required packages are installed
#'
#' @description
#' A method that installs packages from CRAN, if they're not yet installed in
#' the current environment, and loads packages.
#'
#' @param ... Package names as strings
#' @return A logical vector giving `TRUE` if the package is installed and
#' available and `FALSE` if there was an issue with installing each of the
#' specified packages
#' @examples
#' ensure_packages("dplyr", "here")
ensure_packages <- function(...) {
packages <- c(...)
return(vapply(
packages,
function(package) {
if (!(package %in% rownames(installed.packages()))) {
install.packages(
package,
repos = "http://cran.us.r-project.org"
)
}
library(package, character.only = TRUE, warn.conflicts = FALSE)
return(require(package, character.only = TRUE))
},
logical(1)
))
}