initialize_project: Initialize project environment and structure

View source: R/initialize_project.R

initialize_projectR Documentation

Initialize project environment and structure

Description

This function initializes a new R project by setting up folder locations and sourcing code files. It creates a standardized project structure with separate locations for code, results, and data. Results are automatically organized by date, and code can be sourced from specified directories.

Usage

initialize_project(
  env = new.env(),
  home = NULL,
  results = NULL,
  folders_to_be_sourced = "R",
  install_missing_packages = FALSE,
  source_folders_absolute = FALSE,
  encode_from = "UTF-8",
  encode_to = "latin1",
  ...
)

Arguments

env

The environment that the code will be sourced into. Use .GlobalEnv to source code into the global environment. If a different environment is provided, all functions will be sourced into that environment.

home

The folder containing 'Run.R' and 'R/'. This is the main project directory.

results

The base folder for storing results. A subfolder with today's date will be created and accessible via org::project$results_today.

folders_to_be_sourced

Character vector of folder names inside home containing .R files to be sourced into the environment.

install_missing_packages

If TRUE, scans folders_to_be_sourced for package dependencies (via library(), require(), and ⁠pkg::⁠ usage) and installs any missing packages using pak before sourcing. Falls back to install.packages() if pak is not available. Default is FALSE.

source_folders_absolute

If TRUE, folders_to_be_sourced is treated as absolute paths. If FALSE, paths are relative to home.

encode_from

Source encoding for file paths (only used on Windows)

encode_to

Target encoding for file paths (only used on Windows)

...

Additional named arguments for other project folders (e.g., data, raw, etc.)

Details

The function performs several key operations:

  1. Creates necessary directories if they don't exist

  2. Sets up date-based results organization

  3. Sources all .R files from specified directories

  4. Handles path encoding for cross-platform compatibility

  5. Maintains a mirror of settings in org::project

Value

An environment containing:

  • All folder locations as named elements

  • ⁠$env⁠: The environment where code was sourced

  • ⁠$results_today⁠: Path to today's results folder

Examples

## Not run: 
# Initialize a new project
org::initialize_project(
  home = paste0(tempdir(), "/git/analyses/2019/analysis3/"),
  results = paste0(tempdir(), "/dropbox/analyses_results/2019/analysis3/"),
  raw = paste0(tempdir(), "/data/analyses/2019/analysis3/")
)

# Access project settings
org::project$results_today  # Today's results folder
org::project$raw           # Raw data folder

## End(Not run)

org documentation built on April 9, 2026, 9:08 a.m.