devtools::test passes but devtools::check fails

Hello -

I am attempting to add in some unit tests for an R shiny application using the following code.

#make testing genetic matrix; unfortunately it took all of these steps
#to get a file like what I load in - this feels like too many - but works
testing_genetic <- matrix(c(1, "-", 0.2, 0.5, 0.1, 0, "-", 0.3, 0.9, 0.8, 0.2,
                            0.2, "-", 0.5, "-", 0.7), nrow = 4,
                          dimnames = list(c("A", "B", "C", "D"),
                                          c("A", "B", "C", "D")))
testing_genetic <-
testing_genetic <- droplevels(testing_genetic)
testing_genetic <- data.frame(lapply(testing_genetic, as.character),
                              stringsAsFactors = FALSE)
testing_genetic <- tibble::rownames_to_column(testing_genetic, var = ".")

# rename this column to make a matrix with both
testing_genetic[1, 1] <- "A"
testing_genetic[2, 1] <- "B"
testing_genetic[3, 1] <- "C"
testing_genetic[4, 1] <- "D"

#make example tree
testing_tree <- ape::rtree(4)	
new_tiplabels <- c("A", "B", "C", "D")
testing_tree$tip.label <- new_tiplabels

#convert tree to tibble
testing_tree <- tibble::as_tibble(testing_tree)

#confirm tree is tibble 
tibble [7 x 4] (S3: tbl_tree/tbl_df/tbl/data.frame)
 $ parent       : int [1:7] 7 7 6 5 5 5 6
 $ node         : int [1:7] 1 2 3 4 5 6 7
 $ branch.length: num [1:7] 0.15237 0.73568 0.00114 0.3912 NA ...
 $ label        : chr [1:7] "A" "B" "C" "D" ...

#change header to label in order to use dplyr::full_join
testing_genetic <- dplyr::rename(testing_genetic, label = 1)

test_that("data types correct before combining tree", {
  expect_is(testing_tree, "tbl_tree")

 test_that("confirm tree and genetic distance can be combined", {
   expect_silent(dplyr::full_join(testing_tree, testing_genetic, by = "label"))

when running devtools::check this is the error -
cannot coerce class '"phylo"' to a data.frame

  1. tibble::as_tibble(testing_tree)
  2. tibble:::as_tibble.default(testing_tree)
  3., stringsAsFactors = FALSE)

Any thoughts or suggestions for how to overcome this error would be greatly appreciated!

:wave: @jennahamlin!

Generally for such problems (tests passing in a context and not in another) I like to refer to a GitHub thread collecting such cases, where one can often see something relevant. However, I also had a look at your package out of curiosity.

The problem was that the order of packages loading was different in the two contexts. When running tests with devtools::test(), as_tibble() was the function you wanted which is the one from tidytree. When running tests with devtools::check(), as_tibble() was the as_tibble() from tibble. The fix is to add the namespace before the function i.e. write tidytree::as_tibble(), and also to import tidytree in DESCRIPTION (it was already an indirect dependency of your package). I made a PR to your package.

I found this more or less by chance by looking a bit at how one would convert an object of class phylo to a data.frame, and looking at tidytree docs.

As side-notes,

  • in your original test file there's the context() command, that isn't recommended anymore.
  • Also note that testthat::expect_is() is getting superseded in testthat 3.0.0 (not on CRAN yet). To learn more about testthat changes, and if you have one hour free, watch the webinar.

Thank you for the pull request to fix the issue (tidytree::as_tibble; instead of tibble::as_tibble).

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.