繁体   English   中英

quarto rmarkdown 代码块仅显示某些行

[英]quarto rmarkdown code block to only display certain lines

我有一个 .qmd / .rmd 文件,它想要显示代码块的输出。 代码块开头有很多行我想隐藏,在下面的示例中,我希望输出是代码str(month)的第三行并输出str(month)的结果。 我试图编辑代码块参数,但它给了我一个错误:

---
format:
  html: default
---

```{r}
#| echo: c(3)
month <- "July"

str(month)
```

错误:

7: #| echo: c(3)
            ~~~
8: month <- "July"
x The value c(3) is string.
i The error happened in location echo.

rmarkdown支持文件建议这样的事情可能是可能的

我不知道我是否正确理解了这个问题。 但是您可以根据特定块内的行索引选择仅显示您想要的代码。 在 c() {r, echo = c()}中插入要显示的索引行数

您的具体情况

---
format:
  html: default
---

```{r, echo = c(2)}
month <- "July"
str(month) # line 2
```

其他示例:

---
format:
  html: default
---

```{r, echo = c(5,8)}
# Hide
month <- "July"

## Show code and output
str(month) # Line 5

## Show code and output
1+1 # Line 8

## Show just output
2+2

```

这不起作用,因为您使用 YAML 语法作为 Quarto 推荐的块选项,但是#| echo: c(3) #| echo: c(3)不是有效的 YAML。 #| echo: 3 #| echo: 3是。

如有必要,您可以在 YAML 字段中使用!expr来解析 R 代码。 #| echo: !expr c(3) #| echo: !expr c(3)会起作用。 这里解释: https ://quarto.org/docs/computations/r.html#chunk-options

但是,要知道knitr支持其他方式来指定块选项:

  • 选项需要是有效 R 代码的常用标题
```{r, echo = c(3)}
#| echo: c(3)
month <- "July"

str(month)
```
  • 还有它的多行版本,当有像fig.cap这样的长选项时很有用
```{r}
#| rmdworkflow,
#| echo = FALSE,
#| fig.cap = "A diagram illustrating how an R Markdown document
#|   is converted to the final output document.",
#| out.width = "100%"

knitr::include_graphics("images/workflow.png", dpi = NA)
```

有关此新语法的更多信息,请参阅博客文章中公布的内容: https ://yihui.org/en/2022/01/knitr-news/

Github 中也有人问过这个问题 - 那里有更详细的答案: https ://github.com/quarto-dev/quarto-cli/issues/863

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM