While @agstudy's suggestion is correct I've figured out a simple workaround for the bug by using the <<- operator that assigns the variable to the global environment, here's a test-file that works:
set.seed(10)
n <- 11
ds <- data.frame(
y = rnorm(n),
x1 = factor(sample(c("a", "aa", "aaa"), size = n, replace = TRUE)))
suppressMessages(library(rms))
dd <<- datadist(ds)
options(datadist = "dd")
context("rms")
test_that("test", {
fit <- ols(y ~ x1, data=ds)
s <- summary(fit)
expect_true(inherits(s, "summary.rms"))
})
This works also if you happen to do the assignment within the test_that
:
context("rms")
test_that("test", {
set.seed(10)
n <- 11
ds <- data.frame(
y = rnorm(n),
x1 = factor(sample(c("a", "aa", "aaa"), size = n, replace = TRUE)))
suppressMessages(library(rms))
dd <<- datadist(ds)
options(datadist = "dd")
fit <- ols(y ~ x1, data=ds)
s <- summary(fit)
expect_true(inherits(s, "summary.rms"))
})
This is also equivalent to the following code (perhaps easier to understand):
env <- globalenv() # Grab the global environment
env$dd <- datadist(ds) # Assign the datadist to it
If you want to learn more about how environments work I can recommend Hadley's excellent Advanced R coverage of the topic. I found this explaining many of the issues that I was running into.