haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.
https://haozhu233.github.io/kableExtra/
Other
688 stars 147 forks source link

row_spec() is not identifying right row numbers in data frame #370

Open Emm13Bee opened 5 years ago

Emm13Bee commented 5 years ago

I am attempting to create some blank tables in a pdf report. Users of these reports will have some tables which I will pre-poluate with data. Other tables will be for them to complete. In two different tables, I attempt to use the row_spec() function. In the first it works like a charm and the second keeps failing. ee below.

knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(kableExtra)

Working Example

race_eth <- c('American Indian or Alaskan Native',
              'Asian',
              'Black or African American',
              'Hispanic/Latino',
              'Native Hawaiian or Other Pacific Islander',
              'White',
              'Two or More Races ')
groups <- c('Students with Disabilities',
            'Economically Disadvantaged',
            'English Learners')

enrichment <- data.frame(Metric=c("Enrichment Courses Offered",
                                    'Total',
                                  c('Subject area:','Subject area:','Subject area:','Subject area:'),
                                    'Participation in Enrichment Courses',
                                    'Total',
                                    'By Race/Ethnicity',
                                    race_eth,
                                    'By Student Groups',
                                    groups),
                              Number=rep(' ',20),
                              Pct=rep(' ',20),
                              Number_=rep(' ',20),
                              Pct_=rep(' ',20),
                              Number=rep(' ',20),
                              Pct=rep(' ',20),
                           stringsAsFactors = FALSE)

Correctly Formatted Table

kable(enrichment, col.names = c(' ','Number','Percent','Number','Percent','Number','Percent'),align = c('l',rep('c',6))) %>%
  kable_styling("bordered",position = 'l') %>%
  add_header_above(c("ENRICHMENT COURSES","School"=2,"District"=2,"State"=2)) %>%
  row_spec(c(1,7,9,17),bold = T,color = 'white',background = 'blue')

Problem Table

materials <- data.frame(Metric = c("Literacy Materials",
                                    rep(' ',4),
                                   "Mathematics Materials",
                                   rep(' ',4)),
                        update = rep(' ',10),
                        standards = rep(' ',10),
                        responsive = rep(' ',10),
                        title_1 = rep(' ',10),
                           stringsAsFactors = FALSE)

No color appears

kable(materials, escape = F,col.names = linebreak(c('INSTRUCTIONAL MATERIALS','Last\n updated','Level of standards-\n alignment','Level of cultural\n responsiveness','Used for Title I\n and/or Special Ed'),align = 'c')) %>%
  kable_styling("bordered",position = 'l') %>%
  row_spec(c(1,6),bold = T,color = 'white',background = 'blue')

Color not aligned with row numbers appropriately

kable(materials, escape = F,col.names = linebreak(c('INSTRUCTIONAL MATERIALS','Last\n updated','Level of standards-\n alignment','Level of cultural\n responsiveness','Used for Title I\n and/or Special Ed'),align = 'c')) %>%
  kable_styling("bordered",position = 'l') %>%
  row_spec(c(3,9),bold = T,color = 'white',background = 'blue')
haozhu233 commented 5 years ago

Yeah, there is a known bug to in the method I used to solve repeated rows for latex tables. I'm working on that.

Emm13Bee commented 5 years ago

Thanks. I'll anxiously await the fix.

ghxm commented 3 years ago

Has anyone identified a workaround to this problem other than populating all cells with a non-whitespace character?

georgeblck commented 3 years ago

I also just fill the empty cells with an invisible character (\, works for Latex). A fix for this bug would be appreciated however.