gpt4 book ai didi

r - 在 R markdown 中隐藏评论

转载 作者:行者123 更新时间:2023-12-03 15:20:01 26 4
gpt4 key购买 nike

使用 knitr/R markdown 编织时是否可以在代码中隐藏一些注释?示例:

---
title: "SOSO"
author: "SO"
date: '2017-06-06'
output: pdf_document


---

```{r}

# Generate some data

rnorm(2)

## But keep this comment

```
编织时,我希望第一条评论消失,但以某种方式保留第二条。

最佳答案

这是修改钩子(Hook)以更改 knitr 行为的快速示例。

---
title: "SOSO"
author: "SO"
date: 2017-06-06
output: pdf_document
---

```{r setup-hook, echo=FALSE}
hook_in <- function(x, options) {
x <- x[!grepl("^#\\s+", x)]
paste0("```r\n",
paste0(x, collapse="\n"),
"\n```")
}
knitr::knit_hooks$set(source = hook_in)
```

```{r}

# Generate some data
# Lines that starts with `# ` will be removed from the rendered documents

rnorm(2)

## But keep this comment
## But lines that starts with `## ` will be kept

```

产生这个 enter image description here

关于r - 在 R markdown 中隐藏评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44399048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com