Package 'org'

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, create and manage Quarto documents, 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] , CSIDS [cph]
Maintainer: Richard Aubrey White <[email protected]>
License: MIT + file LICENSE
Version: 2024.6.5
Built: 2025-03-19 14:17:40 UTC
Source: https://github.com/csids/org

Help Index


Create example project that uses quarto with results generated outside the .qmd file

Description

Create example project that uses quarto with results generated outside the .qmd file

Usage

create_project_quarto_external_results(home, results)

Arguments

home

Location of the 'home' directory.

results

Location of the 'results' directory.


Create example project that uses quarto with results generated from within the .qmd file

Description

Create example project that uses quarto with results generated from within the .qmd file

Usage

create_project_quarto_internal_results(home, results)

Arguments

home

Location of the 'home' directory.

results

Location of the 'results' directory.


Initialize project

Description

This function initializes the project by setting up folder locations and sourcing code files. It saves folder locations in a new environment and in org::project, which you will use in all of your subsequent code. An additional folder will be created on the user's file system (org::project$results_today) which corresponds to results/YYYY-MM-DD. The sourced folders are saved into org::project$env.

Usage

initialize_project(
  env = new.env(),
  home = NULL,
  results = NULL,
  folders_to_be_sourced = "R",
  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)

home

The folder containing 'Run.R' and 'R/'

results

A folder inside results with today's date will be created and it will be accessible via org::project$results_today (this is where you will store all of your results)

folders_to_be_sourced

The names of folders that live inside home and all .r and .R files inside it will be sourced into the global environment.

source_folders_absolute

If TRUE then folders_to_be_sourced is an absolute folder reference. If FALSE then folders_to_be_sourced is relative and inside home.

encode_from

Folders current encoding (only used on Windows)

encode_to

Folders final encoding (only used on Windows)

...

Other folders that you would like to reference

Details

For more details see the help vignette: vignette("intro", package = "org")

Value

Returns an environment that contains:

  • Folder locations

  • An environment called env into which the code has been sourced into. There is also a side effect where org::project mirrors these values.

Examples

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/")
)
org::project$results_today
org::project$raw

List files and directories

Description

Equivalent to the unix ls command.

Usage

ls_files(path = ".", regexp = NULL)

Arguments

path

A character vector of one or more paths.

regexp

A regular expression that is passed to list.files.

Value

filepaths and directory paths as a character vector


Move directory

Description

Move directory

Usage

move_directory(from, to, overwrite_to = FALSE)

Arguments

from

Filepath or directory path.

to

Filepath or directory path.

overwrite_to

Boolean.


Check if a package is installed and optionally install it

Description

This function checks whether a specified package is installed in the current R environment. Optionally, it can install the package if it is not already installed.

Usage

package_installed(pkg, install_if_missing = FALSE)

Arguments

pkg

A character string specifying the name of the package to check.

install_if_missing

A logical value indicating whether to install the package if it is not installed. Default is FALSE.

Value

A logical value: TRUE if the package is installed (or successfully installed), FALSE otherwise.

Examples

## Not run: 
org::package_installed("data.table")
org::package_installed("ggplot2", install_if_missing = TRUE)

## End(Not run)

Construct path to a file or directory

Description

Construct path to a file or directory

Usage

path(...)

Arguments

...

Character vectors that will be concatenated with "/" as a separator.


Folders to be used/referenced

Description

This environment is used to store the locations of folders that are used in the project.

Usage

project

Format

An environment containing the following elements:

home

The folder containing 'Run.R' and 'R/'

results_today

The folder inside results with today's date, which is created by initialize_project


Set results folder after initialization

Description

This function sets the results folder in the project environment. A folder with today's date will be created inside the results folder.

Usage

set_results(results, proj = org::project)

Arguments

results

A character vector specifying the results folder path(s).

proj

The project environment. Default is org::project.

Value

Nothing. Alters the proj environment to include ⁠$results⁠ and ⁠$results_today⁠.


Write text to a file

Description

This function writes text to a file, optionally including a header at the top of the file. The text and the header are converted from Linux newline format to Windows newline format before writing.

Usage

write_text(
  txt,
  file = "",
  header = "**THIS FILE IS CONSTANTLY OVERWRITTEN -- DO NOT MANUALLY EDIT**\r\n\r\n"
)

Arguments

txt

A character string of text to be written to the file.

file

A character string specifying the file path. Passed through to base::cat. Default is an empty string, which writes to the console.

header

An optional character string header to be inserted at the top of the text file. Default is ⁠**THIS FILE IS CONSTANTLY OVERWRITTEN -- DO NOT MANUALLY EDIT**\r\n\r\n⁠.

Value

No return value. The function is called for its side effect of writing to a file.

Examples

## 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)