首页 > 解决方案 > 在 RStudio 的 R Markdown 中,四个反引号块之后的代码块失去交互性(例如,“运行当前块”按钮消失)

问题描述

我按照 Xie Yihui 的提示R Markdown在文档的输出中逐字呈现代码块。虽然结果看起来不错,但我注意到 RStudio 界面上的格式和交互性发生了变化,也就是说,四个反引号块之后的代码块不再显示为右上角的“运行当前块”等按钮。在下面R Markdown插入。

如何在保持界面交互性的同时继续使用四反引号技巧(或使用具有相同效果的其他技巧)?

---
title: "Diamond sizes"
date: '2016-08-25'
output:
  pdf_document: default
  word_document: default
  html_document: default
---

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

To show a verbatim R code chunk following Xie Yihui's trick ([link](https://yihui.org/en/2017/11/knitr-verbatim-code-chunk/)):

````
`r ''````{r}
library(gapminder)

gapminder %>% ggplot(aes(year, lifeExp, group = country)) +
  geom_line(alpha = 1/3)
```
````

```{r}
library(gapminder)

gapminder %>% ggplot(aes(year, lifeExp, group = country)) +
  geom_line(alpha = 1/3)
```

RStudio 的屏幕截图,其中第一个代码块按预期显示,但第二个代码块没有执行/设置按钮: RStudio 的屏幕截图,其中第一个代码块按预期显示,但第二个代码块没有执行/设置按钮

标签: r-markdown

解决方案


您需要在最后添加最后一个三次反引号。我确实看到了,但是它确实在渲染的文档中添加了 3 个额外的标记,但这就是我所做的,你至少得到了另一个块标记

在此处输入图像描述

更新代码 2021 年 8 月 21 日

---
title: "Untitled"
author: "Daniel"
date: "8/21/2021"
output: html_document
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(dplyr)
```

To show a verbatim R code chunk following Xie Yihui's trick     ([link](https://yihui.org/en/2017/11/knitr-verbatim-code-chunk/)):

````
`r ''````{r}
library(gapminder)

gapminder %>% ggplot(aes(year, lifeExp, group = country)) +
  geom_line(alpha = 1/3)
```
````


```{r}
library(gapminder)

gapminder %>% ggplot(aes(year, lifeExp, group = country)) +
  geom_line(alpha = 1/3)
```    

推荐阅读