R package development: tests pass in console, but fail via devtools::test()
It seems that devtools::test
evaluates the test code in a setting where S4 dispatch does not work in the usual way, at least for packages that you load explicitly in the test file (in this case rstan
). As a result, summary
dispatches to summary.default
instead of the S4 method implemented in rstan
for class "stanfit"
.
The behaviour that you're seeing might relate to this issue on the testthat
repo, which seems unresolved.
Here is a minimal example that tries to illuminate what is happening, showing one possible (admittedly inconvenient) work-around.
pkgname <- "foo"
usethis::create_package(pkgname, rstudio = FALSE, open = FALSE)
setwd(pkgname)
usethis::use_testthat()
path_to_test <- file.path("tests", "testthat", "test-summary.R")
text <- "test_that('summary', {
library('rstan')
stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'
mod <- stan_model(model_code = stancode, verbose = TRUE)
fit <- sampling(mod, data = list(y_mean = 0))
expect_identical(class(fit), structure('stanfit', package = 'rstan'))
expect_true(existsMethod('summary', 'stanfit'))
x <- summary(fit)
expect_error(x$summary)
expect_identical(x, summary.default(fit))
print(x)
f <- selectMethod('summary', 'stanfit')
y <- f(fit)
str(y)
})
"
cat(text, file = path_to_test)
devtools::test(".") # all tests pass
If your package actually imports rstan
(in the NAMESPACE
sense, not in the DESCRIPTION
sense), then S4 dispatch seems to work fine, presumably because devtools
loads your package and its dependencies in a "proper" way before running any tests.
cat("import(rstan)\n", file = "NAMESPACE")
newtext <- "test_that('summary', {
stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'
mod <- stan_model(model_code = stancode, verbose = TRUE)
fit <- sampling(mod, data = list(y_mean = 0))
x <- summary(fit)
f <- selectMethod('summary', 'stanfit')
y <- f(fit)
expect_identical(x, y)
})
"
cat(newtext, file = path_to_test)
## You must restart your R session here. The current session
## is contaminated by the previous call to 'devtools::test',
## which loads packages without cleaning up after itself...
devtools::test(".") # all tests pass
If your test is failing and your package imports rstan
, then something else may be going on, but it is difficult to diagnose without a minimal version of your package.
Disclaimer: Going out of your way to import rstan
to get around a relatively obscure devtools
issue should be considered more of a hack than a fix, and documented accordingly...
`devtools::test()` works, but `devtools::check()` does not for `system.file` to locate file with `.yml` extension
Figured out the problem, finally, the culprit was my .Rbuildignore
- I accidentally kept [.]yml
, [.]yaml
in .Rbuildignore
to ignore the continuous integration yaml files while building the package. I modified them and made them specific, instead of wildcards - and it is working now! Apologies for raising a false alarm; I was quite confused when I got this weird thing. Thanks, @jimhester, who responded on github devtools
issues
r devtools test() errors but testthat test_file() works
Assignment to the global environment is a no-no, see R Inferno and testthat isolates tests as much as possible (see test_that()
details). As a consequence, the optiplum()
assignment to the global environment would not succeed because the testthat function strictly prohibits such behaviour.
@Hadley rightly points out that the function should just return the string instead of assigning it, particularly since it is just two extra characters for each use.
So not
optiplum<-function(){
assign(
x="optiplum",
value=rgb(red=129,green=61,blue=114, maxColorValue = 255),
envir=.GlobalEnv)
}
but
optiplum <- function() rgb(red=102,green=17,blue=109, maxColorValue = 255)
Related Topics
Changing Styles When Selecting and Deselecting Multiple Polygons with Leaflet/Shiny
Create and Call Linear Models from List
Check to See If a Value Is Within a Range
How to Use Variables Newly Created in 'J' in the Same 'J' Argument
Replace Rbind in For-Loop with Lapply? (2Nd Circle of Hell)
How to Rename Element's List Indexed by a Loop in R
Substitute a for B and B for a in a String
Ggplot2: Horizontal Position of Stat_Summary with Geom_Boxplot
Shiny Promises Future Is Not Working on Eventreactive
Downloading Files from Ftp with R
How to Plot Pie Charts in Haplonet Haplotype Networks {Pegas}
Cbind Two Lists of Data.Frames to a New List
Adjusting the Width of Legend for Continuous Variable