| Title: | Organising Projects |
|---|---|
| Description: | A framework for organizing R projects with a standardized structure. Most analyses consist of three main components: code, results, and data, each with different requirements such as version control, sharing, and encryption. This package provides tools to set up and manage project directories, handle file paths consistently across operating systems, organize results using date-based structures, source code from specified directories, and perform file operations safely. It ensures consistency across projects while accommodating different requirements for various types of content. |
| Authors: | Richard Aubrey White [aut, cre] (ORCID: <https://orcid.org/0000-0002-6747-1726>) |
| Maintainer: | Richard Aubrey White <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 2026.4.9 |
| Built: | 2026-06-09 06:02:28 UTC |
| Source: | https://github.com/raubreywhite/org |
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.
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", ... )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", ... )
env |
The environment that the code will be sourced into. Use |
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 |
folders_to_be_sourced |
Character vector of folder names inside |
install_missing_packages |
If |
source_folders_absolute |
If |
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.) |
The function performs several key operations:
Creates necessary directories if they don't exist
Sets up date-based results organization
Sources all .R files from specified directories
Handles path encoding for cross-platform compatibility
Maintains a mirror of settings in org::project
An environment containing:
All folder locations as named elements
$env: The environment where code was sourced
$results_today: Path to today's results folder
## 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)## 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)
This function is equivalent to the Unix ls command but works across platforms.
It can list files and directories matching a regular expression pattern.
ls_files(path = ".", regexp = NULL)ls_files(path = ".", regexp = NULL)
path |
A character vector of one or more paths to search |
regexp |
A regular expression pattern to filter files/directories |
The function:
Handles both single and multiple paths
Supports regular expression filtering
Removes system-specific directories (e.g., @eaDir)
Returns full paths
A character vector of file and directory paths
# List all files in current directory org::ls_files() # List only R files org::ls_files(regexp = "\\.R$") # List files in multiple directories org::ls_files(c("dir1", "dir2"))# List all files in current directory org::ls_files() # List only R files org::ls_files(regexp = "\\.R$") # List files in multiple directories org::ls_files(c("dir1", "dir2"))
Moves a directory and all its contents to a new location. Can optionally overwrite the destination if it already exists.
move_directory(from, to, overwrite_to = FALSE)move_directory(from, to, overwrite_to = FALSE)
from |
Source directory path. |
to |
Destination directory path. |
overwrite_to |
Whether to overwrite existing destination (default: FALSE). |
The function:
Creates the destination directory if it doesn't exist
Copies all files and subdirectories recursively
Removes the source directory after successful copy
Fails if source doesn't exist or destination exists (unless overwrite_to=TRUE)
Nothing. Creates the destination directory and moves all contents.
## Not run: # Move a directory org::move_directory("old_dir", "new_dir") # Move and overwrite existing directory org::move_directory("old_dir", "new_dir", overwrite_to = TRUE) ## End(Not run)## Not run: # Move a directory org::move_directory("old_dir", "new_dir") # Move and overwrite existing directory org::move_directory("old_dir", "new_dir", overwrite_to = TRUE) ## End(Not run)
Joins path components using forward slashes, ensuring proper path formatting across operating systems. Handles multiple components and removes any double slashes that might occur.
path(...)path(...)
... |
Character vectors that will be concatenated with "/" as separator. |
A character vector containing the constructed path.
org::path("home", "user", "data.csv") # Returns "home/user/data.csv" org::path("home//user", "data.csv") # Returns "home/user/data.csv"org::path("home", "user", "data.csv") # Returns "home/user/data.csv" org::path("home//user", "data.csv") # Returns "home/user/data.csv"
An environment that stores the locations of folders used in the project.
projectproject
An environment containing the following elements:
The folder containing 'Run.R' and 'R/'
The folder inside results with today's date, created by initialize_project
Sets the results folder in the project environment and creates a date-based subfolder.
The date-based folder is accessible via proj$results_today and empty date folders
are automatically cleaned up when new results are added.
set_results(results, proj = org::project)set_results(results, proj = org::project)
results |
A character vector specifying one or more possible results folder paths. The first existing path will be used. |
proj |
The project environment. Default is |
Nothing. Modifies the proj environment to include:
The base results folder path
Path to today's results folder (format: YYYY-MM-DD)
Writes text to a file, optionally including a header at the top of the file. Text and header are converted from Linux newline format to Windows newline format before writing.
write_text( txt, file = "", header = "**THIS FILE IS CONSTANTLY OVERWRITTEN -- DO NOT MANUALLY EDIT**\r\n\r\n" )write_text( txt, file = "", header = "**THIS FILE IS CONSTANTLY OVERWRITTEN -- DO NOT MANUALLY EDIT**\r\n\r\n" )
txt |
A character string of text to be written to the file. |
file |
A character string specifying the file path. Passed through to |
header |
An optional character string header to be inserted at the top of the text file.
Default is |
No return value. Called for its side effect of writing to a file.
## Not run: org::write_text("Sample text", "output.txt") org::write_text("Another piece of text", "output.txt", "Custom Header\r\n\r\n") ## End(Not run)## Not run: org::write_text("Sample text", "output.txt") org::write_text("Another piece of text", "output.txt", "Custom Header\r\n\r\n") ## End(Not run)