Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format Code As a Code Block in R Markdown

Tags:

r

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.

like image 788
Parseltongue Avatar asked Dec 05 '17 22:12

Parseltongue


2 Answers

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

like image 146
Len Greski Avatar answered Oct 06 '22 00:10

Len Greski


``` 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

like image 36
emallove Avatar answered Oct 05 '22 23:10

emallove