Home > Back-end >  Wrap Rmarkdown md_output table chunk output in code chunks
Wrap Rmarkdown md_output table chunk output in code chunks

Time:02-04

I want my Rmarkdown, when converted to .md, text chunk output to be wrapped in code ticks (``` * ```).

For example as it is now, an Rmarkdown document like so:

---
title: 'This is a test title'
date: '`r Sys.Date()`'

output:
  md_document: 
    variant: commonmark #or gfm
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  echo = TRUE,
  message = FALSE,
  warning = FALSE
)
``` 

```{r echo=FALSE}
library(palmerpenguins)
```

```{r echo=TRUE}
penguins
```

This is some text.

Rendered with rmarkdown::render("path/to/test.Rmd") to:

```
penguins
```

    ## # A tibble: 344 × 8
    ##    species island    bill_le…¹ bill_…² flipp…³ body_…⁴ sex    year
    ##    <fct>   <fct>         <dbl>   <dbl>   <int>   <int> <fct> <int>
    ##  1 Adelie  Torgersen      39.1    18.7     181    3750 male   2007
    ##  2 Adelie  Torgersen      39.5    17.4     186    3800 fema…  2007
    ##  3 Adelie  Torgersen      40.3    18       195    3250 fema…  2007
    ##  4 Adelie  Torgersen      NA      NA        NA      NA <NA>   2007
    ##  5 Adelie  Torgersen      36.7    19.3     193    3450 fema…  2007
    ##  6 Adelie  Torgersen      39.3    20.6     190    3650 male   2007
    ##  7 Adelie  Torgersen      38.9    17.8     181    3625 fema…  2007
    ##  8 Adelie  Torgersen      39.2    19.6     195    4675 male   2007
    ##  9 Adelie  Torgersen      34.1    18.1     193    3475 <NA>   2007
    ## 10 Adelie  Torgersen      42      20.2     190    4250 <NA>   2007
    ## # … with 334 more rows, and abbreviated variable names
    ## #   ¹​bill_length_mm, ²​bill_depth_mm, ³​flipper_length_mm,
    ## #   ⁴​body_mass_g

This is some text.

How do you get the table (penguins) that is output in the .md document to be wrapped in code ticks (```)?

At the moment, I can get it to work if I use:

---
output: 
  html_document:
    keep_md: TRUE
---

In this example the .md that is generated and kept has all text output surrounded by code ticks. How do I get this without writing an html document?

I've tried updating the s3 object knit_print() but can't figure out how to get it to work. I've also tried various flavors of markdown and looked at pandoc add ons but can't figure it out. I've been googling for hours please help.

CodePudding user response:

A simple approach is to provide a class to class.output chunk option, then the chunk output will be wrapped inside codeticks (triple backticks) automatically.

---
title: 'This is a test title'
date: '`r Sys.Date()`'
output:
  md_document: 
    variant: commonmark #or gfm
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  echo = TRUE,
  message = FALSE,
  warning = FALSE
)
``` 

```{r echo=FALSE}
library(palmerpenguins)
```

```{r echo=TRUE, class.output="output"}
penguins
```

This is some text.

md output

``` r
penguins
```

``` output
## # A tibble: 344 × 8
##    species island    bill_length_mm bill_depth_mm flipper_…¹ body_…² sex    year
##    <fct>   <fct>              <dbl>         <dbl>      <int>   <int> <fct> <int>
##  1 Adelie  Torgersen           39.1          18.7        181    3750 male   2007
##  2 Adelie  Torgersen           39.5          17.4        186    3800 fema…  2007
##  3 Adelie  Torgersen           40.3          18          195    3250 fema…  2007
##  4 Adelie  Torgersen           NA            NA           NA      NA <NA>   2007
##  5 Adelie  Torgersen           36.7          19.3        193    3450 fema…  2007
##  6 Adelie  Torgersen           39.3          20.6        190    3650 male   2007
##  7 Adelie  Torgersen           38.9          17.8        181    3625 fema…  2007
##  8 Adelie  Torgersen           39.2          19.6        195    4675 male   2007
##  9 Adelie  Torgersen           34.1          18.1        193    3475 <NA>   2007
## 10 Adelie  Torgersen           42            20.2        190    4250 <NA>   2007
## # … with 334 more rows, and abbreviated variable names ¹​flipper_length_mm,
## #   ²​body_mass_g
```

This is some text.

CodePudding user response:

Feels a bit hacky, but you can cat the backticks as output to code chunks, but drop the comment symbol (##). (I'm using four backticks for the code blocks below to get the syntax highlighting correct)

````{r echo=FALSE, comment=NA}
 cat('```')
````
````{r echo=FALSE}
penguins
````
````{r echo=FALSE, comment=NA}
 cat('```')
````
  • Related