gpt4 book ai didi

来自变量的 Rmarkdown block 名称

转载 作者:行者123 更新时间:2023-12-04 10:11:25 26 4
gpt4 key购买 nike

如何使用变量作为 block 名称?我有一个被多次调用的子文档,我需要以这样一种方式推进 block 标签,而不是交叉引用它们。

像这样的东西:

child.Rmd

```{r }
if(!exists('existing')) existing <- 0
existing = existing + 1
myChunk <- sprintf("myChunk-%s",existing)
```

## Analysis Routine `r existing`

```{r myChunk,echo = FALSE}
#DO SOMETHING, LIKE PLOT
```

master.Rmd
# Analysis Routines

Analysis for this can be seen in figures \ref{myChunk-1}, \ref{myChunk-2} and \ref{myChunk-3}

```{r child = 'child.Rmd'}
```

```{r child = 'child.Rmd'}
```

```{r child = 'child.Rmd'}
```

编辑可能的解决方案

这是一种潜在的解决方法,灵感来自对所有事物的 SQL 注入(inject)...

child.Rmd
```{r }
if(!exists('existing')) existing <- 0
existing = existing + 1
myChunk <- sprintf("myChunk-%s",existing)
```

## Analysis Routine `r existing`

```{r myChunk,echo = FALSE,fig.cap=sprintf("The Caption}\\label{%s",myChunk)}
#DO SOMETHING, LIKE PLOT
```

最佳答案

在编织和渲染之前将 Rmd 文件预编织到另一个 Rmd 文件中的建议如下

master.Rmd:

# Analysis Routines

Analysis for this can be seen in figures `r paste(paste0("\\ref{", CHUNK_NAME, 1:NUM_CHUNKS, "}"), collapse=", ")`

@@@
rmdTxt <- unlist(lapply(1:NUM_CHUNKS, function(n) {
c(paste0("## Analysis Routine ", n),
paste0("```{r ",CHUNK_NAME, n, ", child = 'child.Rmd'}"),
"```")
}))
writeLines(rmdTxt)

@@@

child.Rmd:
```{r,echo = FALSE}
plot(rnorm(100))
```

编织和渲染 Rmd:
devtools::install_github("chinsoon12/PreKnitPostHTMLRender")
library(PreKnitPostHTMLRender) #requires version >= 0.1.1

NUM_CHUNKS <- 5
CHUNK_NAME <- "myChunk-"
preknit_knit_render_postrender("master.Rmd", "test__test.html")

希望能帮助到你。干杯!

关于来自变量的 Rmarkdown block 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37703326/

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