Search code examples
escapingr-markdowncharacterkable

Rmarkdown and escape characters


How can I print the sequence \\$ in R, as it is? I am working in RStudio, bookdown, inside a kable table in an R chunk. I tried four backslashes before the $ or five backslashes before the $ but did not work.


Solution

  • In order to print \\$, you can set escape = TRUE.

    One .Rmd simple file as an example:

    ---
    title: "Example with escape = TRUE"
    author: bttomio
    output: pdf_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ```{r kable}
    library(kableExtra)
    df <- data.frame("Example" = c("\\\\$"))
    kbl(df, escape = TRUE)
    ```
    

    Output:

    enter image description here