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 <- as.data.frame(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
str(testing_tree)
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
Backtrace:
- tibble::as_tibble(testing_tree)
- tibble:::as_tibble.default(testing_tree)
- base::as.data.frame.default(value, stringsAsFactors = FALSE)
Any thoughts or suggestions for how to overcome this error would be greatly appreciated!