API to 'Python' Library 'pysd'

Using the R package 'reticulate', this package creates an interface to the 'pysd' toolset. The package provides an R interface to a number of 'pysd' functions, and can read files in 'Vensim' 'mdl' format, and 'xmile' format. The resulting simulations are returned as a 'tibble', and from that the results can be processed using 'dplyr' and 'ggplot2'. The package has been tested using 'python3'.

An R wrapper for pysd, using the CRAN reticulate package.

The goal of this package is to allow R users run system dynamics models using pysd, (designed and developed by James Houghton). The pysd project "is a simple library for running System Dynamics models in python, with the purpose of improving integration of Big Data and Machine Learning into the SD workflow."

The pysd system must be installed before installing this package: see pysd installation instructions

pysd2r has been tested with python3, and the following command was used to install pysd from source.

Source link:

python3 setup.py install

Given R's facility for also providing big data and machine learning support, this package opens up the functionality of pysd for R users, and provides an interface to the basic set of methods provided by pysd, including the functions:

  • pysd.read_vensim()
  • model.run()

The API provide by pysd2r includes the following functions (for list of parameters type ?function_name in R) which call the mapping functions in pysd.

  • get_python_info() - Returns the python version currently used by reticulate
  • pysd_connect() - Creates an object to facilitate interaction with pysd
  • read_vensim() - Loads a Vensim simulation file (mdl)
  • read_xmile() - Loads a XMILE simulation file (.xmile)
  • run_model() - Runs a simulation model
  • set_components() - Changes a model parameter
  • get_timestep() - Gets the time step (DT) from the model
  • get_initial_time() - Gets the initial time from the model
  • get_final_time() - Gets the final time from the model
  • set_time_values() - Sets the initial time, final time, and timestep
  • print() - Implementation of generic print function of ipysd S3 object
  • get_doc() - Gets the model variables and returns as a tibble
  • reload_model() - Reloads the original mdl file


You can install pysd2r from github with:


Two Examples

The following example shows how pysd2r can be used to run a simulation model (Population.mdl which is a one-stock Vensim model of population growth).

## basic example code
#> Welcome to package pysd2r.
target <- system.file("models/vensim", "Population.mdl", package = "pysd2r")
py <- pysd_connect()
py <- read_vensim(py, target)
results <- run_model(py)
l <- list("Growth Fraction"=0.02)
out2 <- run_model(py)

The following example shows how pysd2r can be used to run an ensemble of simulations.

#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:plyr':
#>     arrange, count, desc, failwith, id, mutate, rename, summarise,
#>     summarize
#> The following objects are masked from 'package:stats':
#>     filter, lag
#> The following objects are masked from 'package:base':
#>     intersect, setdiff, setequal, union
target <- system.file("models/vensim", "Population.mdl", package = "pysd2r")
gr <- seq(0.01,0.04,by=0.0025)
py <- pysd_connect()
py <- read_vensim(py, target)
ans <- lapply(gr, function (g){
  l <- list("Growth Fraction"=g)
  out     <- run_model(py)
  out$Key <- paste0("GR=",g)
  out     <- select(out,TIME,Population,Key)
full <- rbind.fill(ans)


pysd2r Version 0.1.0

This is the first release of this package.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.1.0 by Jim Duggan, 4 months ago

Browse source code at https://github.com/cran/pysd2r

Authors: Jim Duggan [aut, cre]

Documentation:   PDF Manual  

MIT + file LICENSE license

Imports knitr, reticulate, tibble

Suggests dplyr, ggplot2, testthat

System requirements: 'python3' needs to built for the same architecture R is built for (32 or 64 bit).

See at CRAN