tests/testthat/test-create_rmd_table.R

context("test-create_rmd_table")

# Test data
df1 <- mtcars[1:10, ]
df2 <- mtcars[11:20, ]

# call function
## with column labels
html_tab <- create_rmd_table(x = list(df1, df2),
                  formula = "~ mpg", 
                  column_labels = c("1", "2"))

## without column labels
create_rmd_table(x = list(df1, df2),
                 formula = "~ mpg")

## >2 columns
test_that("output ok", {
  expect_true("kableExtra" %in% class(html_tab))
  expect_true("knitr_kable" %in% class(html_tab))
})

test_that("statistical warnings", {
  expect_warning(create_rmd_table(x = list(df1, df2, df1),
                                  formula = "cyl ~ mpg"))
  expect_error(create_rmd_table(x = list(df1, df2, df1),
                                formula = "~ hp",
                                column_labels = c("1", "2")))
})


# new test data - 2 data frames - same formula - one all na
td1 <- data.frame(gender = sample(c("M", "F"), 5, replace = TRUE),
                  id = 1:5, stringsAsFactors = FALSE)
td2 <- data.frame(gender = rep(NA_character_, 5),
                  id = 1:5, stringsAsFactors = FALSE)

test_that("missing variable drops data set and warns", {
  # one dataset has variable with all missing
  expect_warning(create_rmd_table(x = list(td1, td2),
                                  formula = "~ gender",
                                  column_labels = c("One", "Two"),
                                  var_names = "Gender"))
})


# new test data - 2 data frames different formulas
td3 <- data.frame(id = 1:5,
                  fu_6_months = sample(c("Yes", "No"), 5, replace = TRUE),
                  stringsAsFactors = FALSE)
td4 <- data.frame(id = 1:5,
                  fu_12_months = sample(c("Yes", "No"), 5, replace = TRUE),
                  stringsAsFactors = FALSE)

html_tab <- create_rmd_table(x = list(td3, td4),
                 formula = list("~ fu_6_months",
                                "~ fu_12_months"),
                 column_labels = c("One", "Two"),
                 var_names = "Follow-up")

test_that("output ok", {
  expect_true("kableExtra" %in% class(html_tab))
  expect_true("knitr_kable" %in% class(html_tab))
})


# new test data - 2 data frames - different formulas - one all na
td5 <- data.frame(id = 1:5,
                  fu_12_months = rep(NA_character_, 5),
                  stringsAsFactors = FALSE)

test_that("missing variable drops data set and warns", {
  # one dataset has variable with all missing
  expect_warning(create_rmd_table(x = list(td3, td5),
                                  formula = list("~ fu_6_months",
                                                 "~ fu_12_months"),
                                  column_labels = c("One", "Two"),
                                  var_names = "Follow-up"))
})
JayAchar/hisreportr documentation built on March 18, 2020, 5:57 a.m.