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\"]")`
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
Diagnosing R Package Build Warning: "Latex Errors When Creating PDF Version"
How to Add Boxplots to Scatterplot with Jitter
Replace Characters from a Column of a Data Frame R
Use Superscripts in R Axis Labels
How to Have Conditional Markdown Chunk Execution in Rmarkdown
Dplyr - Summary Table for Multiple Variables
Monitoring for Changes in File(S) in Real Time
Find the Index Position of the First Non-Na Value in an R Vector
Generating a Vector of Difference Between Two Vectors
Pandoc Insert Appendix After Bibliography
Clipping Raster Using Shapefile in R, But Keeping the Geometry of the Shapefile
How to Skip an Error in a Loop
Logistic Regression - Defining Reference Level in R
How to Compute Roc and Auc Under Roc After Training Using Caret in R