[英]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, 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.