styling non-chunk RMarkdown tables

I'm just wondering about Markdown-style tables and how to style them. I understand how to kable_styling() when it comes to data frames formatted from chunk output. But I can't figure out what these are even called:

| rows | col 1 | col 2 | col 3 |
|:-----|------:|------:|------:|
| r1   |  39.3 |  45.5 | 123.4 |
| r2   |  72.9 |  87.1 | 102.6 |
| r3   |  91.0 |  88.5 | 174.3 |

I call them "tables". But I don't think that's a precise enough term, because I've been searching and searching and only come up with examples and documentation dealing with chunk stuff. When I use the df_print: kable option in the front matter, they're kable, but full width, and "condensed".

My workaround to this has been to type my table into a tribble() and render it with kable/kable_styling. This is a bit tedious, and, I don't know, it seems like I'm losing some of the basic power of markdown. Is there an opts_chunk$set(), etc. or some kind of hook that controls these things?

Do you want to include Markdown Tables as-is using markdown syntax in your Rmd document instead of using R functions to create the table ? Is that right ?

You can include such tables directly in Rmd because pandoc know how to handle them. (Pandoc - Pandoc User’s Guide)

However, if you want to style them, you'll need to do according to the output format. CSS for HTML output, latex styling template for pdf output, office template for DOCX or PPTX output.

Using R allow you to style the table using R functions that, in most case, will use the correct styling code for HTML or PDF output. I find it a easier way to style tables, considering the number to formatting table package available in R.

I am in doubt if I understand correctly your issue. What do you want to control with a knitr option exactly ?

My point was that I find myself doing this:

tribble(
   ~rows, ~`col 1`, ~`col2 `, ~`col 3`,
   "r1", 39.3, 45.5, 123.4,
   "r2", 72.9, 87.1, 102.6,
   "r3", 91.0, 88.5, 174.3
) %>% kable() %>% kable_styling() 

and I don't particularly like to do it when this table purely exists as static text. If the answer is playing with the CSS, that's fine, I'm just making sure. I don't have a good grasp on where RMarkdown stops and pandoc begins. Or put another way, I don't know exactly what the limits of RMarkdown are and what can be accomplished with hooks. Therefore when I go beyond those limits, I'm always unsure I'm doing something in an acceptable way. For example, I looked into Pandoc tables and seeing how to assign a CSS class to them, and found this question/answer. Oddly it was there that I saw it mentioned that people will use JQuery to massage the tag attributes after rendering HTML, so that the CSS can be more specific. That's a little mind-blowing to me, because it's the kind of hack I do all the time, but that I don't like to do, because I constantly think there's a better way to do it and I just haven't seen the example yet.

You can accomplish a lot with different hook in knitr on the R side. knitr will help create the .md file for pandoc. Then pandoc will convert the markdown to the output format.

So if you insert directly markdown table, it will directly be handled by pandoc.
For styling table, I think you can include some css specific to some div that you would use to encapsulate you table. Pandoc fenced div can help with that
https://pandoc.org/MANUAL.html#divs-and-spans

With pandoc lua filter is also pretty powerful to manipulate pandoc AST element but is not so easy to master.

At the end, I find that styling table using one of the many R packages is easier with a lot more options that I could use with markdown table.

hope it helps.

2 Likes

Right and as you mentioned, styling with R makes it easier to change the output format of the RMarkdown. I think I'll continue to make use of tribble() + kable(), like I have been doing and remember that advantage next time I think it's tedious. I'm going to try that div-fence pandoc trick. If nothing else, it'd certainly simplify any JQuery hack.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.