tests/testthat/test-l_tab_spanner_delim.R

context("LaTeX -- Ensuring that the `tab_spanner_delim()` function works as expected")

# Create a shortened version of `iris`
iris_short <- iris[1:5, ]

test_that("the `tab_spanner_delim()` function works correctly", {

  # Create a `tbl_latex` object with `gt()`; split the column
  # names into spanner headings and column labels
  tbl_latex <-
    gt(iris_short) %>%
    tab_spanner_delim(delim = ".")

  # Expect a characteristic pattern
  grepl(
    paste0(
      ".*multicolumn\\{2\\}\\{c\\}\\{Sepal\\}.*multicolumn\\{2\\}\\{c\\}\\{Petal\\}",
      ".*cmidrule\\(lr\\)\\{1-2\\}.*cmidrule\\(lr\\)\\{3-4\\}",
      ".*Length & Width & Length & Width & Species.*"),
    tbl_latex %>%
      as_latex() %>% as.character()) %>%
    expect_true()

  # Create a `tbl_latex` object with `gt()`; split the column
  # names into spanner headings and column labels but constrain
  # the splitting only to the `Sepal.Length` and `Sepal.Width` columns
  tbl_latex <-
    gt(iris_short) %>%
    tab_spanner_delim(
      delim = ".",
      columns = c("Sepal.Length", "Sepal.Width")
    )

  # Expect a characteristic pattern
  grepl(
    paste0(
      ".*multicolumn\\{2\\}\\{c\\}\\{Sepal\\} & & &",
      ".*cmidrule\\(lr\\)\\{1-2\\}",
      ".*Length & Width & Petal.Length & Petal.Width & Species.*"),
    tbl_latex %>%
      as_latex() %>% as.character()) %>%
    expect_true()

  # Create a `tbl_latex` object with `gt()`; split the column
  # names into spanner headings and column labels but constrain
  # the splitting only to the `Sepal.Length` and `Sepal.Width`
  # columns using the `vars()` helper
  tbl_latex <-
    gt(iris_short) %>%
    tab_spanner_delim(
      delim = ".",
      columns = vars(Sepal.Length, Sepal.Width)
    )

  # Expect a characteristic pattern
  grepl(
    paste0(
      ".*multicolumn\\{2\\}\\{c\\}\\{Sepal\\} & & &",
      ".*cmidrule\\(lr\\)\\{1-2\\}",
      ".*Length & Width & Petal.Length & Petal.Width & Species.*"),
    tbl_latex %>%
      as_latex() %>% as.character()) %>%
    expect_true()
})

Try the gt package in your browser

Any scripts or data that you put into this service are public.

gt documentation built on Aug. 6, 2020, 1:07 a.m.