README.md

runr: a tidy simulation runner

runr provides an opinionated wrapper to run simulations and recover tidy output.

The packages exports one function, run, which runs a function with some varying parameters, and some fixed parameters. run, expects the varying parameters in a data.frame and the fixed parameters in a list or environment. It returns the results in an data.frame that can be cbind-ed to the input data.frame.

Travis-CI Build Status Coverage Status

Install

devtools::install_github('ashander/runr')

Example Usage:

require(devtools)
#> Loading required package: devtools
devtools::run_examples('.')
#> Updating runr documentation
#> Loading runr
#> Running 2 example files in runr -------------------------------------------
#> Loading runr
#> Running examples in run.Rd ------------------------------------------------
#> > 
#> > growth <- function(n, r, K, b) {
#> +   # Ricker-like growth curve in n = log N
#> +   # this is an obviously-inefficient way to do this ;)
#> +   n  + r - exp(n) / K - b - rnorm(1, 0, 0.1)
#> + }
#> > data <- expand.grid(
#> +                     b = seq(0.01, 0.5, length.out=10),
#> +                     K = exp(seq(0.1, 5, length.out=10)),
#> +                     r = seq(0.5, 3.5, length.out=10)
#> +                     )
#> > initial_data = list(N0=0.9, T=5, reps=10)
#> > 
#> > growth_runner <- function(r, K, b, ic, ...) {
#> +   n0 = ic$N0
#> +   T = ic$T
#> +   reps = ic$reps
#> +   data.frame(n_final = replicate(reps, {for(t in 1:T) {
#> +                     n0 <- growth(n0, r, K, b)
#> +                     };
#> +                   n0}))
#> + }
#> > 
#> > output <- run(data, growth_runner, initial_data)
#> > head(cbind(data, output))
#>            b        K   r    n_final
#> 1 0.01000000 1.105171 0.5 -0.6483548
#> 2 0.06444444 1.105171 0.5 -0.7262741
#> 3 0.11888889 1.105171 0.5 -0.6321369
#> 4 0.17333333 1.105171 0.5 -0.7607913
#> 5 0.22777778 1.105171 0.5 -0.6728537
#> 6 0.28222222 1.105171 0.5 -0.7231543
#> Loading runr

Details

Vignette

For further examples, read the vignette. It focuses on on debug and profile output.

Documentation

Better to install the package and do require(runr); ?run, but here's an ugly preview of the help file:

cat(readLines('man/run.Rd'), sep = '\n')
#> % Generated by roxygen2: do not edit by hand
#> % Please edit documentation in R/run.r
#> \name{run}
#> \alias{run}
#> \title{Run a simulation function}
#> \usage{
#> run(data, fun, fixed_parameters, ...)
#> }
#> \arguments{
#> \item{data}{a data.frame (or tibble, in future)}
#> 
#> \item{fun}{a function}
#> 
#> \item{fixed_parameters}{an environment or list}
#> 
#> \item{...}{additional parameters passed to `fun`}
#> }
#> \value{
#> a data.frame including the columns of data and the return from `fun`
#> }
#> \description{
#> Run a simulation function
#> }
#> \details{
#> `fun` must have signature (a1, a2, <...>, aN, fixed_params, ...) where
#>          1) N is the number of columns in data (the names of these arguments
#>          don't matter). Note <...> elides intervening arguments and is NOT
#>          R's ... parameter! 2) the ... parameter is optional 3) The function
#>          must return a data.frame.
#> }
#> \examples{
#> growth <- function(n, r, K, b) {
#>   # Ricker-like growth curve in n = log N
#>   # this is an obviously-inefficient way to do this ;)
#>   n  + r - exp(n) / K - b - rnorm(1, 0, 0.1)
#> }
#> data <- expand.grid(
#>                     b = seq(0.01, 0.5, length.out=10),
#>                     K = exp(seq(0.1, 5, length.out=10)),
#>                     r = seq(0.5, 3.5, length.out=10)
#>                     )
#> initial_data = list(N0=0.9, T=5, reps=10)
#> 
#> growth_runner <- function(r, K, b, ic, ...) {
#>   n0 = ic$N0
#>   T = ic$T
#>   reps = ic$reps
#>   data.frame(n_final = replicate(reps, {for(t in 1:T) {
#>                     n0 <- growth(n0, r, K, b)
#>                     };
#>                   n0}))
#> }
#> 
#> output <- run(data, growth_runner, initial_data)
#> head(cbind(data, output))
#> }


ashander/runr documentation built on May 10, 2019, 1:52 p.m.