diff --git a/R/govTable.R b/R/govTable.R index 58509c5..fdf54bc 100644 --- a/R/govTable.R +++ b/R/govTable.R @@ -87,13 +87,16 @@ govTable <- function(inputId, df, caption, caption_size = "l", } #Change width of columns if requested - for(i in 1:length(width_overwrite)) { + if(!is.null(width_overwrite)){ + for(i in 1:length(width_overwrite)) { gov_table$children[[2]]$children[[1]][[3]][[1]][[i]]$attribs$class <- paste0( gov_table$children[[2]]$children[[1]][[3]][[1]][[i]]$attribs$class, " govuk-!-width-", width_overwrite[i]) + } } + return(gov_table) } diff --git a/tests/testthat/test-govTable.R b/tests/testthat/test-govTable.R index d23e5a0..4575ad0 100644 --- a/tests/testthat/test-govTable.R +++ b/tests/testthat/test-govTable.R @@ -6,6 +6,7 @@ test_that("table works", { example_data <- data.frame(Months, Bikes, Cars) + # test table with specified widths table_check <- govTable( "tab1", example_data, "Test", "l", num_col = c(2,3), width_overwrite = c("one-half", "one-quarter", "one-quarter")) @@ -25,4 +26,45 @@ test_that("table works", { 3 ) +# test table with unspecified widths + table_check2 <- govTable( + "tab2", example_data, "Test", "l", num_col = c(2,3), + width_overwrite = NULL) + + expect_identical( + table_check2$children[[2]]$children[[1]][[3]][[1]][[1]]$attribs$class, + "govuk-table__header" + ) + + expect_identical( + table_check2$children[[2]]$children[[1]][[3]][[1]][[2]]$attribs$class, + "govuk-table__header govuk-table__header--numeric" + ) + + expect_equal( + length(table_check2$children[[3]]), + 3 + ) + + + # and if the argument isn't mentioned at all + table_check3 <- govTable( + "tab2", example_data, "Test", "l", num_col = c(2,3)) + + expect_identical( + table_check3$children[[2]]$children[[1]][[3]][[1]][[1]]$attribs$class, + "govuk-table__header" + ) + + expect_identical( + table_check3$children[[2]]$children[[1]][[3]][[1]][[2]]$attribs$class, + "govuk-table__header govuk-table__header--numeric" + ) + + expect_equal( + length(table_check3$children[[3]]), + 3 + ) + + })