2
votes

I'm having a problem with kableExtra collapse rows function. When I use the collapse rows function to collapse repetitive words in my table, it also messes with the formatting of my head.

If I use the code:

kable(caption = "Salmonella in Routine Food Samples")

for my data I get the following:

Original heading:

enter image description here

You can see from this image that the caption "Salmonella in Routine Food Samples is bold and centered" but that the rows also repeat certain words. So I used the following command to cut the rows

    kable(caption = "Salmonella in Routine Food Samples") %>% 
collapse_rows(columns = 1:4, valign = "top") 

This code successfully squashed the rows but now if you look at the image below it has also edited my kable caption.

Formatting error:

enter image description here

It seems that the collapse rows function is also editing the format of the kable caption, I tried to repair this by fixing it using row_spec but it doesn't work. I tried two solutions.

Solution attempt 1:

    kable(caption = "Salmonella in Routine Food Samples") %>% 
collapse_rows(columns = 1:4, valign = "top") %>% 
row_spec(row = 0, bold = TRUE, align = "center")

This code has no effect on fixing the caption formatting, in fact it only applies it to the first row under the caption. I thought it might be due to an order issue so I rearranged the code to do the formatting before the collapse rows. So I tried the following:

Solution attempt 2:

    kable(caption = "Salmonella in Routine Food Samples") %>% 
   row_spec(row = 0, bold = TRUE, align = "center") %>%
   collapse_rows(columns = 1:4, valign = "top")

This code also fails to fix the heading, it seems that no matter what I do, using the collapse_rows function also changes the formatting of my kable caption.

In addition the code

collapse_rows(columns = 1:4, valign = "top") %>%
   row_spec(row = 0, bold = TRUE, align = "center")

Only ever applies this formatting to the first "row" of my table but it still also messes up the caption formatting. That is, it'll bold and centre the first row of my table whilst the collapse_rows is still changing my caption heading from bold and centre to left aligned and non bolded. So I don't understand why "collapse rows" treats the caption heading as a "Row" but the "row_spec" does not treat the caption heading as a row?

Has anyone else ever experience this problem and found a work around?

Apologises if this is confusing, this is my first time posting and I'm very new to R. Any help greatly appreciated! Thank you.

1

1 Answers

0
votes

A friend gave me a workaround so I shall share it here in case anyone else is having this issue.

On the code line kable(caption = "Salmonella in Routine Food Samples")

Change the code to the following:

kable(caption = "<center><strong>Salmonella in Routine Food Samples</strong></center>")

if you wish to make the headings bigger use the H function e.g h1, h2, h3 etc, so the code should look like this for larger headings:

kable(caption = "<h3><center><strong>Salmonella in Routine Food Samples</strong></center></h3>")

This will specify how you want your caption heading to appear. So in my example, it's bold and centred. If I add in the "h3" part, it increases the caption heading size. Once this line is in, collapse rows shall not mess up the formatting again.