Learn R Programming

sdbuildR: Easily Build, Simulate, and Explore Stock-and-Flow Models in R

sdbuildR is an R package for building, simulating, and exploring stock-and-flow models. Originating in the field of system dynamics, stock-and-flow models represent processes as quantities (stocks) that accumulate or deplete over time and the processes (inflows and outflows) that change them. sdbuildR is designed to make stock-and-flow modelling accessible to a broad audience, requiring minimal mathematical or system dynamics background knowledge.

Quick start

Load one of the dozens of models from the built-in library, such as the classic SIR (Susceptible-Infected-Recovered) epidemic model:

library(sdbuildR)

# Load stock-and-flow model
sfm <- stockflow("SIR")

# View the stock-and-flow diagram
plot(sfm)

# Simulate and visualise the dynamics over time
simulate(sfm) |> plot()

simulate() produces an interactive timeseries plot. See it run — and build the same model from scratch — in the Get started guide.

Installation

The release version can be installed from CRAN:

install.packages("sdbuildR")

The development version can be installed from GitHub:

if (!require("remotes")) install.packages("remotes")
remotes::install_github("KCEvers/sdbuildR")

Overview of main features

sdbuildR is designed to support the iterative process of building, simulating, and testing stock-and-flow models. Models can flexibly be modified and simulated in either R or Julia (see setup guide) for a major speed-up on large or repeated runs. All package capabilities are described in the vignettes:

  • Get started: A guided tour of the main features.
  • Build: Build, modify, and simulate stock-and-flow models.
  • Ensemble simulations: Explore a model’s behaviour across parameter ranges and initial conditions.
  • Unit tests: Verify models behave as intended with unit tests.
  • Job Demands-Resources Theory: An example of formalizing psychological theory with sdbuildR.
  • Julia setup: Set up Julia for faster simulations.
  • Import/Export: Import models from deSolve or Insight Maker, and export to other formats.

Other system dynamics software

sdbuildR is heavily based on common system dynamics software such as Vensim, Powersim, Stella, and Insight Maker. To translate xmile models to R, see the R package readsdr. To build stock-and-flow models with the R package deSolve, the book System Dynamics Modeling with R by Jim Duggan will prove useful. In Python, stock-and-flow models are supported by PySD.

Troubleshooting

sdbuildR is under active development. While thoroughly tested, the package may have bugs, particularly in complex model translations. We encourage users to report issues on GitHub - your input helps the package improve! Use summary() to run model diagnostics, and use the vignettes for guidance.

Citation

To cite sdbuildR, please use:

citation("sdbuildR")
#> To cite package 'sdbuildR' in publications use:
#> 
#>   Evers K (2025). _sdbuildR: Easily Build, Simulate, and Visualise
#>   Stock-and-Flow Models_. doi:10.32614/CRAN.package.sdbuildR
#>   <https://doi.org/10.32614/CRAN.package.sdbuildR>, R package version
#>   1.0.8.
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Manual{,
#>     title = {sdbuildR: Easily Build, Simulate, and Visualise Stock-and-Flow Models},
#>     author = {Kyra Caitlin Evers},
#>     year = {2025},
#>     note = {R package version 1.0.8},
#>     doi = {10.32614/CRAN.package.sdbuildR},
#>   }

Copy Link

Version

Install

install.packages('sdbuildR')

Monthly Downloads

492

Version

2.0.0

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Kyra Caitlin Evers

Last Published

June 16th, 2026

Functions in sdbuildR (2.0.0)

indexof

Find index of value in vector or string
insightmaker_to_json

Convert .InsightMaker file to .json file
import_insightmaker

Import Insight Maker model
hill

Hill function
head.verify_stockflow

Print first rows of verify results
import_desolve

Import a deSolve model
install_julia_env

Install, update, or remove Julia environment
interpret

Recursively interpret a parsed R expression
length_IM

Length of vector or string
op_precedence

Get the precedence level of an operator
plot.ensemble_stockflow

Plot timeseries of ensemble simulation
lookup

Add or modify lookup variables (graphical functions)
has_internet

Check if user has internet
logit

Logit function
needs_parens

Determine whether parentheses are needed to preserve meaning
maybe_wrap

Conditionally wrap a result string in parentheses
logistic

Logistic function
meta

Modify meta of stock-and-flow model
head.simulate_stockflow

Print first rows of a simulation
nonnegative

Check whether x is less than zero
print.compare_stockflow

Print comparison of two stock-and-flow models
print.stockflow

Print overview of stock-and-flow model
plot.verify_stockflow

Plot verify results
print.simulate_stockflow

Print simulation of a stock-and-flow model
print.summary_stockflow

Print method for summary_stockflow
plot.simulate_stockflow

Plot timeseries of simulation
rbool

Generate random logical value
plot.stockflow

Plot stock-and-flow diagram
pulse

Create pulse function
ramp

Create ramp function
round_IM

Round values half-up (as in Insight Maker)
rem

Remainder and modulus
sim_settings

Modify simulation specifications
stock

Add or modify stocks
seasonal

Create a seasonal wave function
sim_methods

Translate between deSolve and DifferentialEquations.jl solver names
saveat_func

Internal function to save data frame at specific times
step

Create step function
rdist

Generate random number from custom distribution
simulate.stockflow

Simulate stock-and-flow model
update.stockflow

Create or modify variables
summary.stockflow

Run model diagnostics
tail.verify_stockflow

Print last rows of verify results
summary.simulate_stockflow

Summarise simulation results
url_to_insightmaker

Extract Insight Maker model from URL
tail.simulate_stockflow

Print last rows of a simulation
unit_test

Add or modify unit tests
unit_tests

Display unit tests defined on a stock-and-flow model
use_julia

Start Julia and activate environment
stockflow

Create a new stock-and-flow model
verify

Verify model behavior with unit tests
verify.stockflow

Verify unit tests against simulation results
change_type

Change variable type
as.data.frame.verify_stockflow

Convert verify() results to a data frame
as.data.frame.stockflow

Convert stock-and-flow model to data frame
clean_name

Clean variable name(s)
constant

Add or modify constants
as.data.frame.simulate_stockflow

Create data frame of simulation results
contains_IM

Check whether value is in vector or string
change_name

Change name of variable
compare_models

Compare two stock-and-flow models
auxiliary

Add or modify auxiliaries
expit

Expit function
ensemble

Run ensemble simulations
find_named_arg

Find a named argument in a list of call arguments
flow

Add or modify flows
dependencies

Find dependencies
discard_unit_test

Remove a unit test from a stock-and-flow model
export_model

Export a stock-and-flow model
discard

Remove variable(s)
export_plot

Save plot to a file
custom_func

Create or modify custom variables or functions