Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renv snapshot + setup; remove duplicate R project #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions Burgerpeiling.Rproj

This file was deleted.

1 change: 1 addition & 0 deletions R/.Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
1 change: 1 addition & 0 deletions R/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
session_info.txt
4 changes: 2 additions & 2 deletions R/01 IMPORT.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

#-----------------------------------------------------------------------------------------------

#package isolation in project-specific environment
use_renv <- FALSE #change to TRUE to use renv (default: FALSE)
# Package isolation in project-specific environment
use_renv <- TRUE

# Load necessary libraries
source('SRC/packages.R')
Expand Down
110 changes: 48 additions & 62 deletions R/SRC/packages.R
Original file line number Diff line number Diff line change
@@ -1,121 +1,107 @@

#-----------------------------------------------------------------------------------------------

#Setup and packages
# Setup and packages

#-----------------------------------------------------------------------------------------------

#clear console
# Clear console
cat("\014")

#garbage collection
# Garbage collection
gc(verbose = FALSE, full = TRUE)

#detect the number of available CPU cores minus one core for system stability
# Detect the number of available CPU cores minus one core for system stability
cpu_cores <- parallel::detectCores() - 1
options(Ncpus = cpu_cores)
cat(sprintf("CPU cores dedicated to this pipeline: %d\n", getOption("Ncpus", 1L)),"\n")

#package repository platform
options(repos = c(CRAN = "https://cran.r-project.org"))
# Set package repository platform
if (!use_renv)
options(repos = c(CRAN = "https://cran.r-project.org"))

message("deploy packages")
message("Loading packages...")

if (use_renv) {
#install and load the renv package (if not already installed)
# Install and load the 'renv' package (if not already installed)
if (!requireNamespace("renv", quietly = TRUE)) {
utils::install.packages("renv")
}
library(renv)

#initialize or create an renv.lock file
if (!file.exists("renv.lock")) {
renv::init()
}
# Restore packages as recorded in the lockfile
renv::restore()
}

#-----------------------------------------------------------------------------------------------

# load required libraries
# Define vector of all to-be-loaded packages:
libraries <- c(
#external packages (not-being from cran-repo or similar)
'devtools',
#Functions for Base Types and Core R and 'Tidyverse' Features
# Functions for base types, core R, and 'tidyverse' features:
'rlang',
#tools
# Tools:
'tools',
#Relative paths
# Relative paths:
'here',
#sssentials
# Essentials:
'tidyverse','janitor','scales',
#dataframe extension
# Dataframe extension:
'data.table',
#spss
# SPSS
'haven', 'labelled',
#Read Rectangular Text Data
# Read rectangular text data:
'readr',
#statistical calculations
# Statistical calculations:
'stats',
#multiuple response sets
# Multiple response sets:
'expss',
#colour scheme
# Colour scheme
'viridis',
#layout plots
# Layout plots (not used):
#'patchwork',
#read and write xlsx
# Read and write .xlsx files:
'openxlsx',
#publication-ready analytical and summary tables
# Publication-ready analysis and summary tables:
'gtsummary',
#survey data-processing
# Survey data-processing:
'survey',
'srvyr',
#file system operations
# File system operations:
'fs',
#CBS api
# CBS API:
'cbsodataR',
#scales
# Scales:
'scales',
#chart creation
# Chart creation:
'esquisse',
#parallel processing
# Parallel processing:
'multidplyr',
#mapping functions for parallel processing
#'furr',
# Mapping functions for parallel processing:
'furrr',
'parallel',
#imputation
# Multiple imputation for missing values:
'mice',
#self-organised maps
# Self-organised maps:
'kohonen',
#ensemble clustering
# Ensemble clustering:
'diceR',
#optimal binning
# Optimal binning:
'optbin'
)

# Install and load missing packages
missing_libraries <- libraries[!libraries %in% installed.packages()]
if (length(missing_libraries) > 0) {
install.packages(missing_libraries)
}
lapply(libraries, library, character.only = TRUE, quietly = TRUE)


# Load the furrr package for parallel processing
if (!requireNamespace("furrr", quietly = TRUE)) {
#install.packages("furrr")
devtools::install_github("DavisVaughan/furrr")
}
library(furrr)

#update the renv.lock file with the package dependencies
if (use_renv) {
renv::snapshot()
if (!use_renv) {
missing_libraries <- libraries[!libraries %in% installed.packages()]
if (length(missing_libraries) > 0) {
install.packages(missing_libraries)
}
}

#optionally restore the environment with the specified packages
# renv::restore()
# Load packages
lapply(libraries, library, character.only = TRUE, quietly = TRUE)

#-----------------------------------------------------------------------------------------------

#review packages loaded (store active-packages set-up)
sessionInfo() %>% capture.output(file="session_info.txt")
# Review packages loaded and write to file (i.e., store active-packages set-up)
sessionInfo() %>% capture.output(file="session_info.txt")

message("Done loading packages.")
Loading