Rendering a data frame as a human-readable table in a .md file

Maybe a silly question but is there a way to render a data frame in a R Markdown document as a human-readable table in a .md file?

I'll like to display an example of a rendered table as a code chunk on a pkgdown site. I'm rendering a temporary .rmd file in a temporary directory and then read the generated .md file to show the user what output to expect.

Currently, using knitr::kable()

```{r}
#| echo: false

data.frame(x = 1:2, y = letters[1:2]) |>
  knitr::kable("simple")
```

produces an HTML table which is not easy to read:

<table>
<thead>
<tr class="header">
<th style="text-align: right;">x</th>
<th style="text-align: left;">y</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: left;">a</td>
</tr>
<tr class="even">
<td style="text-align: right;">2</td>
<td style="text-align: left;">b</td>
</tr>
</tbody>
</table>

I'd prefer to have a layout as follows:

  x  y  
---  ---
  1  a  
  2  b  

Is there a way to do that?

you could try "pipe" or "rst" instead of "simple"

pipe produces the same output as simple in this case. I hadn't tried rst and it almost produces something acceptable. The only issue is that the table doesn't align well:

=== === x y  
=== === 1 a  
2 b  
=== ===

I also tried jira and org that I hadn't tried before. jira also produces something acceptable but has the same problem as rst.

This is overly elaborate, though it does work; hopefully someone can improve on this.

---
title: "test_kable"
output: md_document
date: "2024-07-18"
---
```{r echo=FALSE, paged.print=FALSE}
cat(paste0(capture.output(knitr::kable(data.frame(x = 1:2, y = letters[1:2]),format="rst")),collapse="\n"))
```
    ## 
    ## 
    ## ===  ===
    ##   x  y  
    ## ===  ===
    ##   1  a  
    ##   2  b  
    ## ===  ===
1 Like

Thanks for you effort.

Since I'm showing the raw .rmd together with the rendered output, I'd prefer not to do these kind of transformations because they might confuse the users. I could of course use two separate chunks, one to display for the user and the other to render but that would be overkill. If there's no ways to do it with a particular argument or function, I'll simply not add that example.

Using knitr::kable() with pipe or simple will output a Markdown table.

Though it will output as raw markdown to be converted into any output format (HTML, PDF, etc..)

So it all depend how you want to show the output.

But if you convert to a .md file and then import this .md content as a markdown code cell it should not be converted to HTML.

I'm not sure to follow. What do you mean by 'import' here? How would you do that with the example in the first post? The output need to remain in a code chunk.

You said here that your are reading the generated .md. That is why I am talking about import.

In your first post, the fact that a HTML table is produced means that somehow the content was parsed by Pandoc and converted to HTML. I am guessing this because knitr::kable("simple") should output markdown not HTML.

I see. My initial post wasn't precise enough and I realize my example wasn't good either. I'm actually rendering a temporary R markdown document in an R subprocess and then read and display the markdown output from it, all this within an R code chunk. The actual example uses a table of citation keys that need to be processed by pandoc, hence my problem. A real life examples can be found here and its output there.

So I'll inevitably end up with the problem of alignement mentioned above if I use a format that pandoc doesn't convert to HTML like rst.

This can be simulated with:

'
  ---
  bibliography: %s
  ---

  ```{r}
  #| include: false
  library(pakret)
  ```

  ```{r}
  #| echo: false
  pkrt_list("withr", "readr") |> 
    as.data.frame() |> 
    knitr::kable(format = "rst")
  ```

  ## References
' |> 
  pakret:::dedent() |> 
  pakret:::local_files() |>
  pakret:::read_local_file(target = "md") |>
  cat()
#> ======= ======= ========= Package Version Reference ======= =======
#> ========= withr 3.0.0 Hester et al. (2024)  
#> readr 2.1.5 Wickham, Hester, and Bryan (2024)  
#> ======= ======= =========
#> 
#> ## References
#> 
#> Hester, Jim, Lionel Henry, Kirill Müller, Kevin Ushey, Hadley Wickham,
#> and Winston Chang. 2024. *Withr: Run Code ’with’ Temporarily Modified
#> Global State*. <https://CRAN.R-project.org/package=withr>.
#> 
#> Wickham, Hadley, Jim Hester, and Jennifer Bryan. 2024. *Readr: Read
#> Rectangular Text Data*. <https://CRAN.R-project.org/package=readr>.

Created on 2024-08-29 with reprex v2.1.0

I guess we can consider my problem solved.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.