Search code examples
rflextable

How to remove a column with NA value from flextable in R


I created a data frame containing full year and year to date summaries. Since the current year , say 2022, has no full year data, so it would be NA in the pivot table under the 2022 column.

However using flextable through tabulator, 2022 will still show up in the output such as below.

ct <- tabulator(
  x = test, rows = c("Sex", "Measure", "AgeGroup"),
  columns = "timer",
  `2019` = as_paragraph(as_chunk(Yr2019, formatter = as.integer)), 
  `2020` = as_paragraph(as_chunk(Yr2020, formatter = as.integer)),
  `2021` = as_paragraph(as_chunk(Yr2021, formatter = as.integer)),
  `2022` = as_paragraph(as_chunk(Yr2022, formatter = as.integer))
)
ft <- as_flextable(ct)
ft

How can I remove the 2022 column under full year?

enter image description here


Solution

  • OK. I found a solution, which is just to delete the attribute column inside ct$visible_columns

    ct$visible_columns <- ct$visible_columns[-8, ]

    or

    ct$visible_columns <- ct$visible_columns[ct$visible_columns$col_keys != "@Full year@2022",].

    Then the output would appear as

    enter image description here