在R Markdown中将代码格式化为代码块

7

有没有办法在R Markdown中像Stackoverflow等许多网站提供的“代码”格式中格式化代码。

我希望能够做到以下操作:

任何采用以下形式的模型 income ~ education + parental_income ...

我想在R markdown中模拟灰色文本,以便在最终编织文档时易于阅读。


10
RMarkdown 是 Markdown 的超集(包含所有 Markdown 特性 + 独有的额外特性),因此在文档中使用反引号,例如 \income ~ education + parental_income``,应该与 Markdown 一样可以正常工作。 - Marius
哇,如果是真的那就太棒了。 - Parseltongue
3个回答

8

可以使用反引号或缩进4个空格来生成等同于HTML <code> </code> 标签块的效果。这里有一个Rmd文档,说明了这种行为。

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

......和输出结果。不幸的是,我必须包含一张图片来展示它正确地呈现。

在此输入图片描述


6

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接