gpt4 book ai didi

r - 如何在 .rmd 文件中获取部分名称?

转载 作者:行者123 更新时间:2023-12-04 03:44:27 25 4
gpt4 key购买 nike

是否可以将部分的名称捕获为变量?

我有一个 .rmd 文档,其中每个部分都遵循相同的格式,例如,

# The Beginning


### What we know about The Beginning


### What we need to learn about The Beginning

...

我想写一些更笼统的东西,比如

# The Beginning


### What we know about `r section_name`


### What we need to learn about `r section_name`

有没有办法捕获当前部分名称?

最佳答案

如果您将一个 block 命名为“The Beginning”,您可以在整个 Rmd 中使用 knitr::all_labels() 引用它,例如

---
title: "Test"
author: "Jared_Mamrot"
date: "22/12/2020"
output: html_document
---

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

# The Beginning
```{r The Beginning}
summary(cars)
```

### What we know about `r knitr::all_labels()[2]`
```{r 3rd_chunk}
# Refer to the 'label' inside the chunk
knitr::all_labels()[2]
```

### What we need to learn about `r knitr::all_labels()[2]`
```{r 4th_chunk}
cars %>%
ggplot(aes(x = speed, y = dist)) +
geom_point() +
ggtitle(label = knitr::all_labels()[2]) +
theme_bw()
```

当我编织 Rmd 时,knitr::all_labels()[2] 被替换为“The Beginning”

example_knitted.png

编辑:重新阅读您的问题后,我意识到这不是一个完整的答案。也许你可以以某种方式适应 this function for printing a Table of Contents获取节标题并在编织文档中使用它。

关于r - 如何在 .rmd 文件中获取部分名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65400518/

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