Testthat fails when setting up rms by calling datadist() + options()

r, testthat

Solution

yes It is a scope problem as suggested by the error.

A possible work around is to define your `ds` where you call `test_dir`

for example You create file, `runtest.R` like this

library(rms)
set.seed(10)
ds <- data.frame(
  ftime = rexp(200),
  fstatus = sample(0:1,200,replace=TRUE),
  x1 = runif(200),
  x2 = runif(200),
  x3 = factor(sample(LETTERS[1:3], size=200, replace=TRUE)))
ddist <- datadist(ds)
options(datadist="ddist")
library(testthat)
test_dir("inst/tests")

Problem

I'm trying to do some unit testing using the `testthat` package but I can't seem to get it to work properly together with the `rms` package. The following example: ``` library(rms) set.seed(10) ds <- data.frame( ftime = rexp(200), fstatus = sample(0:1,200,replace=TRUE), x1 = runif(200), x2 = runif(200), x3 = factor(sample(LETTERS[1:3], size=200, replace=TRUE))) ddist <- datadist(ds) options(datadist="ddist") s <- Surv(ds$ftime, ds$fstatus == 1) fit <- cph(s ~ x1 + x2 + x3, data=ds) ``` returns this error: Error in Design(eval.parent(m)) : dataset ddist not found for options(datadist=) This even though print(ddist) works and the options("datadist") returns the proper variable. Does `testthat` have a different variable scope that causes errors? Update I run the testthat by a R console started in the my package dir (Eclipse StatET): ``` library(testthat) test_dir("inst/tests") q() ``` The same error occurs with the R CMD check --as-cran

Original source