MyNixOS website logo
Description

Hierarchical Runtime Configuration Management.

Provides tools for managing nested, multi-level configuration systems with runtime mutability, type validation, and default value management. Supports creating hierarchical options managers with customizable validators for scalar and vector types (numeric, character, logical), enumerated values, bounded ranges, and complex structures like XY pairs. Options can be dynamically modified at runtime while maintaining type safety through validator functions, and easily reset to their default values when needed.

R-CMD-check.yaml CRAN_Status_Badge

optree

A lightweight R package for hierarchical runtime configuration

optree provides a flexible, mutable, hierarchical options manager for R. It allows packages and projects to define nested configuration options, enforce group validation rules, and easily reset to defaults. Ideal for complex, interdependent settings like crop models, phenology simulations, or multi-module projects.


Features

  • Hierarchical, nested options (a.b.c)
  • Dot-separated path notation for both get() and set() operations
  • Runtime mutable configuration
  • Merge-aware updates (update only part of a nested group)
  • Transactional updates (rollback on validation failure)
  • Group and field validation for consistency
  • Reset all options to defaults with one call
  • Minimal dependencies, lightweight and easy to use

Installation

From CRAN

install.packages('optree')

From GitHub (development version)

remotes::install_github('byzheng/optree')

Documentation

For detailed documentation and reference, visit: https://optree.bangyou.me


Getting Started

  1. Create an options manager
library(optree)

# Create the CANOLA options manager with built-in validators
canola <- create_options_manager(
  defaults = list(
    phenology = list(
      thermaltime = list(
        x = c(2, 30, 35),
        y = c(0, 28, 0)
      )
    ),
    frost_threshold = 0,
    model_name = "canola_v1",
    backend = "cpu"
  ),
  validators = list(
    "phenology.thermaltime" = v_xypair(min_len = 3),
    "frost_threshold" = v_numeric_range(min = -5, max = 5),
    "model_name" = v_character_scalar(),
    "backend" = v_enum(c("cpu", "gpu"))
  )
)
  1. Access options
# Get a single leaf
canola$get("phenology.thermaltime.x")

# Get the entire group
canola$get("phenology.thermaltime")
  1. Update options
# Method 1: Use dot-separated paths (NEW!)
canola$set("phenology.thermaltime.x" = c(5,25,40))
canola$set("phenology.thermaltime.y" = c(0,20,0))

# Method 2: Use nested list (traditional way)
canola$set(phenology = list(
  thermaltime = list(
    x = c(10,20,30),
    y = c(0,10,0)
  )
))

# Mix both styles in one call
canola$set(
  "phenology.thermaltime.x" = c(15,25,35),
  frost_threshold = -2
)

# Update top-level option
canola$set(frost_threshold = -2)

Validator example:

# v_enum validator - value must be one of the allowed choices
canola$set(backend = "quantum")
# Error: must be one of: cpu, gpu

# v_numeric_range validator - frost_threshold must be between -5 and 5
canola$set(frost_threshold = 10)
# Error: must be between -5 and 5

# v_xypair validator - x and y must have same length and meet min/max length constraints
canola$set(phenology = list(thermaltime = list(
  x = c(1, 2),
  y = c(0, 1, 2)
)))
# Error: 'x' and 'y' must have the same length

# v_character_scalar validator - model_name must be non-empty string
canola$set(model_name = "")
# Error: must not be empty
  1. Transactional safety
# Set valid values
canola$set("phenology.thermaltime.x" = c(5,25,40))
canola$set("phenology.thermaltime.y" = c(0,20,0))

# Try an invalid update - will fail and rollback
canola$set("phenology.thermaltime.x" = c(1,2))  # length mismatch
# Error: x and y must have same length

# Options remain unchanged after failed validation
canola$get("phenology.thermaltime.x")  # Still c(5,25,40)
  1. Reset options
canola$reset()
# Returns all defaults
canola$get()

Advantages over existing approaches

Featureoptreesettings::options_managerbase R options()
Hierarchical optionsLimited
Merge-aware updates
Runtime mutable
Group validationCustom only
Arbitrary depthLimited

Built-in Validators

optree provides a collection of ready-to-use validators for common validation patterns:

  • v_numeric_scalar() – Validates single numeric values
  • v_numeric_range(min, max) – Validates numeric values within a range
  • v_numeric_vector(min_len, finite) – Validates numeric vectors with length/finiteness constraints (min_len must be a non-negative whole number; finite must be a single logical)
  • v_logical_scalar() – Validates single logical values
  • v_character_scalar() – Validates single non-empty character values
  • v_enum(choices) – Validates that value is one of predefined choices
  • v_xypair(min_len, max_len = NULL) – Validates paired x/y lists with optional upper length bound (max_len must be NULL or a non-negative whole number)

For more details and examples, see the Validators Vignette.

Contributing

Contributions are welcome! Please submit issues or pull requests via GitHub.

License

MIT License – see LICENSE file for details.

Metadata

Version

0.1.1

License

Unknown

Platforms (78)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows