Reputation: 73
Using R Markdown, I'm creating PDF file which includes a summary table with a dynamically changing/unknown number of monthly columns. I'd like the "Month Actuals" label to adjust dynamically to appear above all month columns present at the time the report is run.
I created a month_count object:
library(tidyverse)
month_count <-
data %>%
summarize(month_count = n_distinct(year_month)) %>%
as_vector()
and used in the add_header_above function:
summary %>%
kbl("latex",
booktabs = TRUE) %>%
kable_styling(position = "left",
latex_options = c("HOLD_position",
"scale_down",
"striped")) %>%
column_spec(column = 1, width = "2in") %>%
column_spec(column = 2:99, width = ".75in") %>%
add_header_above(c(" ", "Year-to-Date" = 4, "Month-to-Date" = 3, "Monthly Actuals" = month_code))
This works, however, the label on the output reads "Monthly Actuals.month_count." Any thoughts on how either to 1) improve this label or 2) set dynamically adjusting header labels?
Below is are the data in play if that is useful information for troubleshooting labeling:
data
year_month
<chr>
2021_07
2021_08
2021_09
2021_10
2021_11
2021_12
2022_01
2022_02
summary
` Area `Yearly Target` `YTD Total` `Percent of Yearly T~` Balance `Monthly Target` `Current Month~` `Percent of Mo~` `2021 07` `2021 08`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
Area 1 416 0 0 -416 35 0 0 0 0
Area 2 520 69 13.3 -451 44 9 20.4 12 11
Area 3 624 0 0 -624 52 0 0 0 0
Area 4 50 45 90 -5 5 7 140 5 3
Area 5 0 6713 0 0 0 959 0 0 1097
Area 6 0 240 0 0 0 30 0 21 42
Area 7 125 6 4.8 -119 11 2 18.2 1 2
Area 8 120 18 15 -102 10 5 50 0 0
Area 9 3900 1127 28.9 -2773 325 141 43.4 71 96
Area 10 54 0 0 -54 5 0 0 0 0
Area 11 0 118 0 0 0 15 0 50 35
Upvotes: 0
Views: 204
Reputation: 1784
Your month_count
is a named integer, and that name ends up in the header.
> str(month_count)
Named int 8
- attr(*, "names")= chr "month_count"
Two examples on how to resolve this
names(month_count) <- NULL
and keep everything else as ismonth_count
at all and rely on ncol(summary)
instead: add_header_above(c(" ", "Year-to-Date" = 4, "Month-to-Date" = 3, "Monthly Actuals" = ncol(summary)-8))
With either of these,the additional text in the header is removed:
Upvotes: 1