How do I embed formatted R code in table cells in rmarkdown?

154 views Asked by At

I am working on a vignette for an R package where I want to show how to do the same thing in two R packages. My preferred approach would be to include several tables, where I can show the appropriate code next to each other, something like so:

enter image description here

I don't care about the exact fonts, but I would like to see the R code laid out nicely and clearly, like it would be in a regular rmarkdown chunk, but then in a cell of a table.

Is there a way to achieve something like this in rmarkdown?

(I imagine it can by done by typing html directly, but that would be very cumbersome because I don't know html very well.)

2

There are 2 answers

1
Daniel_j_iii On

If I understand your question correctly, you just want the R code to be able to be displayed in Rmarkdown?

`r ''````{r}
setup <- pacakge2::gogo(x) %>% 
dplyr::filter(blabla2)
packages2::plot(setup)
```

This allows you to show your code in an R chunk enter image description here

If you want to show your entire code you could do this

````markdown
`r ''````{r}
steup <- pacakge2::gogo(x) %>% 
dplyr::filter(blabla2)
packages::plot(setup)
```
````

enter image description here

Which actually renders your r code as markdown code

0
dash2 On

How to do it with my huxtable package. You'll need at least version 5.0.

library(huxtable)
hx <- huxtable(
        c("", "Goal A", "Goal B", "Goal C"), 
        c("Package 1", "", "", ""), 
        c("Package 2", "", "", "")
      )
hx[2, 2] <- "`setup <- package1::start_run()`"
markdown(hx)[2, 2] <- TRUE
quick_pdf(hx)

Result:

Screenshot