gpt4 book ai didi

r - Rmarkdown 中的循环 : How to make an in-text figure reference? 图标题?

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

{r setup, include=FALSE, message=FALSE, results="hide"}
knitr::opts_chunk$set(echo = TRUE)
图书馆(针织)
图书馆(kfigr)
图书馆(dplyr)
图书馆(PNG)
图书馆(网格)
图书馆( Pandas )
图书馆(ggplot2)

问题

rmarkdown 中的循环:文本中的图形引用?图标题?

目标

使用 for 循环创建包含文本、文本内结果和多个图形引用以及图形列表中相关图形标题的部分。数字引用/编号应该与这些新部分前后的数字无缝衔接。

注意: for 循环中引用的图形是在文本中较早生成的,保存为 png,然后重新加载。就此示例而言,这可能看起来很笨拙,但实际的无花果是 map 并且生成速度很慢(我打算在我按照自己的方式获得数字后注释掉生成数字的循环)。

{r echo = FALSE, warnings=FALSE, message=FALSE, results="hide"}

数据:每年我们都有不同数量的地层,因此需要循环。

df <- rbind(
data.frame(strata = rep("A", 10), x = rnorm(10, mean= 10), y = rnorm(10, mean = 15),z = rnorm(10, mean = 20)),
data.frame(strata = rep("B", 10), x = rnorm(10, mean= 5), y = rnorm(10, mean = 10), z = rnorm(10, mean = 15)),
data.frame(strata = rep("C", 10), x = rnorm(10, mean= 15), y = rnorm(10, mean = 20), z = rnorm(10, mean = 10)))

first_plot:for 循环按层创建截面之前应出现在列表中的图形

first_plot <- ggplot(df, aes(x, fill=strata)) + geom_histogram()

last_plot:for 循环按层创建截面后应出现在列表中的图形

last_plot <- ggplot(df, aes(x = strata, y = z)) + geom_boxplot()

图形生成(一旦我有了我想要的 map ,这部分将在我的版本中被注释掉)

strat <- unique(df$strata)

for (i in seq_along(strat)) {
sub <- df %>% filter(strata %in% strat[i])
fig1 <- ggplot(sub, aes(x = x, y = y)) + geom_point()
ggsave(fig1, file=paste0("fig1_", strat[i], ".png"))
fig2 <- ggplot(sub, aes(x = x, y = z)) + geom_point()
ggsave(fig2, file=paste0("fig2_", strat[i], ".png"))
}

加载png的

df_figs <- list.files(pattern = "\\.png$")
for (i in df_figs){
name <- gsub("-",".",i)
name <- gsub(".png","",name)
i <- paste(".\\",i,sep="")
assign(name,readPNG(i))
}

简介部分

报告中的一些介绍性文字和一张图 r figr('first_plot',TRUE, type='Figure')

```{r echo = FALSE, warnings=FALSE, message=FALSE, results = "asis"}

# Summary of results and image file names that will be references in text
results <- df %>%
group_by(strata) %>%
dplyr::summarise_each(funs(mean)) %>%
mutate(fig1 = paste0("fig1_", strata),
fig2 = paste0("fig2_", strata))

#Text template (each strata will have its own section)
template <- "# The %s stratum
The mean of *x* in %s stratum was %1.1f. Relationships between *x* and *y* and *x* and *z* can be found in `r figr('%s', TRUE, type='Figure')` and `r figr('%s', TRUE, type='Figure')`.

"

#Create markdown sections in for loop
for(i in seq(nrow(results))) {
current <- results[i, ]
cat(sprintf(template,
current$strata, current$strata,
current$x,
current$fig1, current$fig2))
}

#Also doesn't work:
template <- "# The %s stratum
The mean in %s stratum was %1.0f. Results can be found in "
template2 <- " and "
template3 <- ".

"

`figr('%s', TRUE, type='Figure')` and `figr('%s', TRUE, type='Figure')`."

#For loop
for(i in seq(nrow(results))) {
current <- results[i, ]
cat(sprintf(template,
current$strata, current$strata,
current$mean,
current$fig_1, current$fig_2))
print(paste0("`r figr(",paste0("'", current$fig1,"'"), TRUE, type='Figure'))
cat(sprintf(template2))
print(paste0("`r figr(",paste0("'", current$fig2,"'"), "TRUE, type='Figure'),`"))
cat(sprintf(template3))
}
```

结论部分

报告和图中的一些讨论文本r figr('last_plot',TRUE, type='Figure')

数字

*NOTE:* I don't know how to automate the looped portion of the list of figures here, so I've done it by hand.

```{r 'first_plot', echo=FALSE, warning=FALSE, fig.width=6.5, fig.height=6, fig.cap="The caption for the first figure."}
suppressMessages(print(first_plot))
```

```{r 'fig1_A', echo=FALSE, warning=FALSE, fig.width=6.5, fig.height=6, fig.cap="Caption text for fig1_A."}
grid.raster(fig1_A)
```

```{r 'fig2_A', echo=FALSE, warning=FALSE, fig.width=6.5, fig.height=6, fig.cap="Caption text for fig2_A."}
grid.raster(fig2_A)
```

```{r 'fig1_B', echo=FALSE, warning=FALSE, fig.width=6.5, fig.height=6, fig.cap="Caption text for fig1_B."}
grid.raster(fig1_B)
```

```{r 'fig2_B', echo=FALSE, warning=FALSE, fig.width=6.5, fig.height=6, fig.cap="Caption text for fig2_B."}
grid.raster(fig2_B)
```

```{r 'fig1_C', echo=FALSE, warning=FALSE, fig.width=6.5, fig.height=6, fig.cap="Caption text for fig1_C."}
grid.raster(fig1_C)
```

```{r 'fig2_C', echo=FALSE, warning=FALSE, fig.width=6.5, fig.height=6, fig.cap="Caption text for fig2_C."}
grid.raster(fig2_C)
```

```{r 'last_plot', echo=FALSE, warning=FALSE, fig.width=6.5, fig.height=6, fig.cap="The caption for the last figure."}
suppressMessages(print(last_plot))
```

最佳答案

解决方案

  1. 使用knit_expand()
  2. 使用captioner代替kfigr
  3. 这会在正文和报告末尾为您的数字(或表格)编号。
  4. 此脚本向您展示如何在 for 循环中创建 Markdown 段落,这些段落在文本中引用了图形。
  5. 它还向您展示了如何在保留数字顺序的同时在 for 循环中创建自定义图形标题。
  6. 如果您展示如何使用 brew 执行#4 和#5,我会给您所有的 SO 点。

图书馆

库(knitr)
图书馆(dplyr)
图书馆(PNG)
图书馆(网格)
图书馆( Pandas )
图书馆(ggplot2)
库(开发工具)
库(字幕)

使用 captioner 包创建一个 fig_nums() 函数( https://github.com/adletaw/captioner/blob/master/vignettes/using_captioner.Rmd )

fig_nums <- captioner(prefix = "Figure")

数据

每年我们都有不同数量的地层,因此需要循环。

df <- rbind(
data.frame(strata = rep("A", 10), x = rnorm(10, mean= 10), y = rnorm(10, mean = 15), z = rnorm(10, mean = 20)),
data.frame(strata = rep("B", 10), x = rnorm(10, mean= 5), y = rnorm(10, mean = 10), z = rnorm(10, mean = 15)),
data.frame(strata = rep("C", 10), x = rnorm(10, mean= 15), y = rnorm(10, mean = 20), z = rnorm(10, mean = 10)))

first_plot:for 循环按层创建截面之前应该出现在列表中的图形

first_plot <- ggplot(df, aes(x, fill=strata)) + geom_histogram()
fig_nums("first_plot", display = FALSE)

last_plot:for 循环按层创建截面后应出现在列表中的图形

last_plot <- ggplot(df, aes(x = strata, y = z)) + geom_boxplot()

图形生成

一旦您对无花果有了自己想要的效果,请将此部分注释掉。如果您在 R 中进行大量映射,则此步骤不会令人费解、不自然、次优、不必要或像一个非常糟糕的主意。

strat <- unique(df$strata)

for (i in seq_along(strat)) {
sub <- df %>% filter(strata %in% strat[i])
fig1 <- ggplot(sub, aes(x = x, y = y)) + geom_point()
ggsave(fig1, file=paste0("fig1_", strat[i], ".png"))
fig2 <- ggplot(sub, aes(x = x, y = z)) + geom_point()
ggsave(fig2, file=paste0("fig2_", strat[i], ".png"))
}

加载png的

df_figs <- list.files(pattern = "\\.png$")
for (i in df_figs){
name <- gsub("-",".",i)
name <- gsub(".png","",name)
i <- paste(".\\",i,sep="")
assign(name,readPNG(i))
}

简介

报告中的一些介绍性文字和一张图 r fig_nums("first_plot", display="cite")

将在文本中引用的结果和图像文件名:

```{r echo = FALSE, warnings=FALSE, message=FALSE, results = "asis"}

results <- df %>%
group_by(strata) %>%
dplyr::summarise_each(funs(mean)) %>%
mutate(fig1 = paste0("fig1_", strata),
fig2 = paste0("fig2_", strata))

```

```{r run-numeric-md, warning=FALSE, include=FALSE}

#The text for the markdown sections in for loop... the knit_expand() is the work-horse here.

out = NULL
for (i in as.character(unique(results$strata))) {
out = c(out, knit_expand(text=c('#### The *{{i}}* strata',
'\n',
'The mean of *x* is ',
'{{paste(sprintf("%1.1f", results$x[results$strata==i]))}}', '({{fig_nums(results$fig1[results$strata==i],display="cite")}}).',
'\n'
)))
}

```

为每个层创建部分

`r paste(knit(text = out), collapse = '\n')`

结论

报告和图中的一些讨论文本r fig_nums("last_plot",display="cite")

图表列表

`r fig_nums("first_plot",caption="Here is the caption for the first figure.")`

```{r 'first_plot', echo=FALSE, warning=FALSE, fig.width=6.5, fig.height=6}
suppressMessages(print(first_plot))
```

```{r figcaps, include=FALSE}
caps = NULL

for (i in as.character(unique(results$strata))) {
caps = c(caps, knit_expand(
text=c({{fig_nums(results$fig1[results$strata==i], caption="Caption text for strata *{{i}}* goes here.")}},
'``` {r {{results$fig1[results$strata==i]}}, echo=FALSE, warning=FALSE, fig.width=6.5, fig.height=6}',
{{paste0('grid.raster(',results$fig1[results$strata==i],')')}},
'```',
'\n')))
}

#DON'T FORGET TO UNLIST!
src <- unlist(caps)
```

`r paste(knit(text = src),sep='\n')`

`r fig_nums("last_plot", caption="The caption for the last figure.")`

```{r 'last_plot', echo=FALSE, warning=FALSE, fig.width=6.5, fig.height=6}
suppressMessages(print(last_plot))
```

关于r - Rmarkdown 中的循环 : How to make an in-text figure reference? 图标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41308960/

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