How to Display Verbatim Inline R Code with Backticks Using Rmarkdown

How to display verbatim inline r code with backticks using Rmarkdown?

Here is a trick that I use. First, note \x60 is `:

> cat('\x60', '\n')
`

Then you write

`r '\x60r foo+bar\x60'`

which will give you `r foo+bar` in the markdown output, but that will become r foo+bar in the HTML output, so you need to protect the backticks in markdown, using two (or more) backticks. Then you end up with this hairball:

`` `r '\x60r foo+bar\x60'` ``

Your own solution is good, but I'd just define

rinline <- function(code) {
sprintf('``` `r %s` ```', code)
}

Also see this post for another trick.

Verbatim code chunks with double quotation marks in RMarkdown

You can escape the double quotes:

---
title: "Untitled"
author: "CLRR"
date: "2020/6/20"
documentclass: article
output:
bookdown::pdf_document2:
latex_engine: xelatex
keep_tex: TRUE
---

This verbatim can appear in the output:

`r knitr::inline_expr("coef(summary(model))[\"(Intercept)\", \"Estimate\"]")`

Sample Image

In the comments, @monte provides the other solution, which is alternating single and double quotes: knitr::inline_expr('coef(summary(model))["(Intercept)", "Estimate"]')

How to output literal backticks in knitr::spin

The correct answer was posted by rawr in the comments (he only missed r and a tick mark):

snippet spin.header
`r paste("#' ---")`
`r paste("#' author: 'ENTER'")`
`r paste("#' title: 'ENTER'")`
`r paste("#\' date: '\x60r Sys.time()\x60'")`
`r paste("#' output:")`
`r paste("#' html_document")`
`r paste("#' ---")`

Use a variable name with spaces inline in R markdown

In this instance can use normal quotes,

 The first five values of your data are `r df$"your data"[1:5]`

or rather

 The first five values of your data are `r df[["your data"]][1:5]`


Related Topics



Leave a reply



Submit