'how to center kable title/caption while keeping rows left aligned in R kableextra

I want to have the title(caption) of the table centered, but the values of columns left assigned. I also want my caption to be broken into two lines and each line to be centered.

align option changes both of them in a same way.

head(mtcars) %>%
   kbl(caption = "{first row\\\\  
                    second row}",
          align = "l") %>%
   kable_styling()


Solution 1:[1]

Maybe this helps:

head(mtcars) %>%    
    kbl(align = "l") %>% 
    add_header_above(data.frame("{first row \\\\ second row}", 12), 
                     monospace = TRUE) %>%
    kable_styling()

The result:

enter image description here

Update

I assume that you are trying to produce a table in html format. To center the caption and put a line break on the caption texts, you can use html elements: <center> for centering and <br> for line breaking.

head(mtcars) %>%
    kbl(caption = "<center>{first row <br>
                  second row}</center>", 
        align = "l") %>% 
    kable_styling()

The result:

enter image description here

References

  1. Format captions in kableExtra()
  2. https://developer.mozilla.org/en-US/docs/Web/HTML/Element

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1