diff --git a/R/tab_remove.R b/R/tab_remove.R index 4201780fbb..1d5c8cfaf7 100644 --- a/R/tab_remove.R +++ b/R/tab_remove.R @@ -301,7 +301,8 @@ rm_spanners <- function( as.integer( resolve_vector_i( expr = {{ spanners }}, - vector = spanners_tbl[["spanner_id"]] + vector = spanners_tbl[["spanner_id"]], + "spanner" ) ) @@ -498,7 +499,8 @@ rm_footnotes <- function( as.integer( resolve_vector_i( expr = {{ footnotes }}, - vector = as.character(seq_len(footnotes_count)) + vector = as.character(seq_len(footnotes_count)), + "footnote" ) ) @@ -649,7 +651,8 @@ rm_source_notes <- function( as.integer( resolve_vector_i( expr = {{ source_notes }}, - vector = as.character(seq_len(source_notes_length)) + vector = as.character(seq_len(source_notes_length)), + "source note" ) ) diff --git a/tests/testthat/_snaps/tab_remove.md b/tests/testthat/_snaps/tab_remove.md index daaffe948e..fe735eb100 100644 --- a/tests/testthat/_snaps/tab_remove.md +++ b/tests/testthat/_snaps/tab_remove.md @@ -103,6 +103,20 @@ Output [1] "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n \n \n \n
yearbdy_stylehphp_rpmtrqtrq_rpmmpg_cmpg_hmsrp
GT2017coupe647625055059001118447000
458 Speciale2015coupe597900039860001317291744
458 Spider2015convertible562900039860001317263553
458 Italia2014coupe562900039860001317233509
488 GTB2016coupe661800056130001522245400
California2015convertible553750055747501623198973
GTC4Lusso2017coupe680825051457501217298000
FF2015coupe652800050460001116295000
" +--- + + Code + t_sp <- exibble %>% gt() %>% tab_spanner("span1", columns = c(char, fctr)) + rm_spanners(t_sp, "span2") + Condition + Error in `rm_spanners()`: + ! Can't find spanner `span2` in the data. + Code + rm_spanners(t_sp, c("span1", "span2", "span3")) + Condition + Error in `rm_spanners()`: + ! Can't find spanners `span2` and `span3` in the data. + # Table footnotes can be removed using `rm_footnotes()` Code @@ -131,6 +145,26 @@ Output [1] "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n \n \n \n \n \n \n
numcharfctrdatetimedatetimecurrencyrowgroup
1 1.111e-01apricotone2015-01-1513:352018-01-01 02:2249.950row_1grp_a
2.222e+00bananatwo2015-02-1514:402018-02-02 14:3317.950row_2grp_a
3.333e+01coconutthree2015-03-1515:452018-03-03 03:441.390row_3grp_a
4.444e+02durianfour2015-04-1516:502018-04-04 15:5565100.000row_4grp_a
5.550e+03NAfive2015-05-1517:552018-05-05 04:001325.810row_5grp_b
NAfigsix2015-06-15NA2018-06-06 16:1113.255row_6grp_b
7.770e+05grapefruitsevenNA19:102018-07-07 05:22NArow_7grp_b
8.880e+06honeydeweight2015-08-1520:20NA0.440row_8grp_b
1 Footnote 1
" +--- + + Code + t_ft <- exibble %>% gt() %>% tab_footnote(footnote = "Footnote 1", locations = cells_body( + 1, 1)) %>% tab_footnote(footnote = "Footnote 2", locations = cells_body(1, 2)) + t_ft %>% rm_footnotes(footnotes = 0:1) + Condition + Error in `rm_footnotes()`: + ! The following footnote indices do not exist in the data: 0. + Code + t_ft %>% rm_footnotes(footnotes = 2:3) + Condition + Error in `rm_footnotes()`: + ! The following footnote indices do not exist in the data: 3. + Code + t_ft %>% rm_footnotes(footnotes = 3) + Condition + Error in `rm_footnotes()`: + ! The following footnote indices do not exist in the data: 3. + # Table source notes can be removed using `rm_source_notes()` Code @@ -159,3 +193,23 @@ Output [1] "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n \n \n \n \n \n \n \n
numcharfctrdatetimedatetimecurrencyrowgroup
1.111e-01apricotone2015-01-1513:352018-01-01 02:2249.950row_1grp_a
2.222e+00bananatwo2015-02-1514:402018-02-02 14:3317.950row_2grp_a
3.333e+01coconutthree2015-03-1515:452018-03-03 03:441.390row_3grp_a
4.444e+02durianfour2015-04-1516:502018-04-04 15:5565100.000row_4grp_a
5.550e+03NAfive2015-05-1517:552018-05-05 04:001325.810row_5grp_b
NAfigsix2015-06-15NA2018-06-06 16:1113.255row_6grp_b
7.770e+05grapefruitsevenNA19:102018-07-07 05:22NArow_7grp_b
8.880e+06honeydeweight2015-08-1520:20NA0.440row_8grp_b
Source Note 1
" +--- + + Code + t_sn <- exibble %>% gt() %>% tab_source_note(source_note = "Source Note 1") %>% + tab_source_note(source_note = "Source Note 2") + t_sn %>% rm_source_notes(source_notes = 0:1) + Condition + Error in `rm_source_notes()`: + ! The following source note indices do not exist in the data: 0. + Code + t_sn %>% rm_source_notes(2:3) + Condition + Error in `rm_source_notes()`: + ! The following source note indices do not exist in the data: 3. + Code + t_sn %>% rm_source_notes(3) + Condition + Error in `rm_source_notes()`: + ! The following source note indices do not exist in the data: 3. + diff --git a/tests/testthat/test-tab_remove.R b/tests/testthat/test-tab_remove.R index b7089f6464..80ec16b735 100644 --- a/tests/testthat/test-tab_remove.R +++ b/tests/testthat/test-tab_remove.R @@ -526,27 +526,14 @@ test_that("Spanner column labels can be removed using `rm_spanners()`", { # Expect an error if the `spanners` vector contains ID values that # don't exist for any spanner column labels - expect_error( - gtcars %>% - dplyr::select( - -mfr, -trim, bdy_style, drivetrain, - -drivetrain, -trsmn, -ctry_origin - ) %>% - dplyr::slice(1:8) %>% - gt(rowname_col = "model") %>% - tab_spanner(label = "HP", columns = c(hp, hp_rpm)) %>% - tab_spanner(label = "Torque", columns = c(trq, trq_rpm)) %>% - tab_spanner(label = "MPG", columns = c(mpg_c, mpg_h)) %>% - tab_spanner( - label = "Performance", - columns = c( - hp, hp_rpm, trq, trq_rpm, - mpg_c, mpg_h - ) - ) %>% - rm_spanners(spanners = c("HP", "perf")) %>% - render_as_html() - ) + expect_snapshot(error = TRUE, { + t_sp <- exibble %>% + gt() %>% + tab_spanner("span1", columns = c(char, fctr)) + + rm_spanners(t_sp, "span2") + rm_spanners(t_sp, c("span1", "span2", "span3")) + }) }) test_that("Table footnotes can be removed using `rm_footnotes()`", { @@ -605,26 +592,16 @@ test_that("Table footnotes can be removed using `rm_footnotes()`", { # Expect an error when providing any integer values that don't correspond # with the available footnotes ([1, 2]) - expect_error( - exibble %>% + expect_snapshot(error = TRUE, { + t_ft <- exibble %>% gt() %>% tab_footnote(footnote = "Footnote 1", locations = cells_body(1, 1)) %>% - tab_footnote(footnote = "Footnote 2", locations = cells_body(1, 2)) %>% - rm_footnotes(footnotes = 0:1) - ) - expect_error( - exibble %>% - gt() %>% - tab_footnote(footnote = "Footnote 1", locations = cells_body(1, 1)) %>% - tab_footnote(footnote = "Footnote 2", locations = cells_body(1, 2)) %>% - rm_footnotes(footnotes = 2:3) - ) - expect_error( - exibble %>% - gt() %>% - tab_footnote(footnote = "Footnote 1", locations = cells_body(1, 1)) %>% - rm_footnotes(footnotes = 2) - ) + tab_footnote(footnote = "Footnote 2", locations = cells_body(1, 2)) + t_ft %>% rm_footnotes(footnotes = 0:1) + t_ft %>% rm_footnotes(footnotes = 2:3) + t_ft %>% rm_footnotes(footnotes = 3) + }) + expect_no_error( exibble %>% gt() %>% @@ -731,26 +708,17 @@ test_that("Table source notes can be removed using `rm_source_notes()`", { # Expect an error when providing any integer values that don't correspond # with the available source notes ([1, 2]) - expect_error( - exibble %>% + expect_snapshot(error = TRUE, { + t_sn <- exibble %>% gt() %>% tab_source_note(source_note = "Source Note 1") %>% - tab_source_note(source_note = "Source Note 2") %>% - rm_source_notes(source_notes = 0:1) - ) - expect_error( - exibble %>% - gt() %>% - tab_source_note(source_note = "Source Note 1") %>% - tab_source_note(source_note = "Source Note 2") %>% - rm_source_notes(source_notes = 2:3) - ) - expect_error( - exibble %>% - gt() %>% - tab_source_note(source_note = "Source Note 1") %>% - rm_source_notes(source_notes = 2) - ) + tab_source_note(source_note = "Source Note 2") + + t_sn %>% rm_source_notes(source_notes = 0:1) + t_sn %>% rm_source_notes(2:3) + t_sn %>% rm_source_notes(3) + }) + expect_no_error( exibble %>% gt() %>%