library(rutilities)
library(magrittr)
context("Checking Data Names and Classes")
# -----------------------------------------------------------------------------
# Try to input something that isn't a tibble ----------------------------------
inc_data <- list(x = c(1,2), y = c(3,4,5))
test_that("Checking that non-tibbles return errors",{
expect_error(check_names_and_classes(inc_data, NA),
"Error: the input data needs to be a tibble")
})
# Try to input names or classes that aren't right -----------------------------
# Example data
correct_data <- tibble::tibble(x = c(1,2), y = c("a", "b"))
# Incorrect lists
inc_list1 <- list(x = "character", y = "numeric")
inc_list2 <- list(x = "numeric")
inc_list3 <- list(x = "numeric", y = "character", z = "logical")
# If the names and classes do not match, return an error
test_that("Checking that incorrect name-class pairs return errors",{
expect_error(
check_names_and_classes(correct_data, inc_list1),
stringr::str_c("The names and/or classes aren't correct.\n\n",
"Name-class pairs in data, but not given:\n",
"$x\n",
"[1] \"numeric\"\n\n",
"$y\n",
"[1] \"character\"\n\n\n",
"Name-class pairs given, but not in data:\n",
"$x\n",
"[1] \"character\"\n\n",
"$y\n",
"[1] \"numeric\"\n"),
fixed = TRUE
)
expect_error(
check_names_and_classes(correct_data, inc_list2),
stringr::str_c("The names and/or classes aren't correct.\n\n",
"Name-class pairs in data, but not given:\n",
"$y\n",
"[1] \"character\"\n"),
fixed = TRUE
)
expect_error(
check_names_and_classes(correct_data, inc_list3),
stringr::str_c("The names and/or classes aren't correct.\n\n",
"Name-class pairs given, but not in data:\n",
"$z\n",
"[1] \"logical\"\n"),
fixed = TRUE
)
})
# If the names and classes do match, return confirmation message
correct_list1 <- list(y = "character", x = "numeric")
correct_list2 <- list(x = "numeric", y = "character")
test_that("Checking that input names and classes are correct",{
expect_identical(
check_names_and_classes(correct_data, correct_list1), correct_data
)
expect_identical(
check_names_and_classes(correct_data, correct_list2), correct_data
)
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.