7

Is there anyway to format code in the "code" format offered by many websites like Stackoverflow in R Markdown.

I want to be able to do something like the following:

Any model that takes the form income ~ education + parental_income ...

I want to emulate that greyed out text in R markdown for ease of readability when I eventually knit the document.

Parseltongue
  • 11,157
  • 30
  • 95
  • 160
  • 10
    RMarkdown is a superset of Markdown (i.e. includes all Markdown features + extra unique ones), so using backticks like `\`income ~ education + parental_income\`` in your document should work the same way. – Marius Dec 05 '17 at 22:39
  • Wow. Huge if true. – Parseltongue Dec 05 '17 at 22:40

3 Answers3

7

One can use backticks or indenting by 4 spaces to generate the equivalent of the HTML <code> </code> tag block. Here is an Rmd document illustrating this behavior.

---
title: "Sample Document"
author: "bigNumber"
date: "12/5/2017"
output: html_document
---

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

## R Markdown

here is some example code written in R Markdown. If we want to enter a code block we use the backtick, such as `inc ~ ed + occ`. 

If we want a code block with multiple lines, indenting by 4 spaces also works

    # first line of code
    for(i in 1:100) {
       # do something here
    }

More text after the code block ends. 

...and the output. Unfortunately I have to include an image to show that it renders correctly.

enter image description here

Len Greski
  • 10,505
  • 2
  • 22
  • 33
5

``` If you'd rather not indent your code block by four spaces, you can use triple back-ticks on separate lines. ```

https://bookdown.org/yihui/rmarkdown/markdown-syntax.html#block-level-elements

emallove
  • 1,417
  • 1
  • 17
  • 22
0

html could work if you want to have a string with gray color as a background

Changing chunk background color in RMarkdown

smurfit89
  • 327
  • 5
  • 17