本文介绍了R:你如何以程序的方式将剧情嵌入到RMarkdown的标签中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我可以使用RMarkdown的 {。tabset} 嵌入地块 #### Heading {.tabset} #####副标题1 ```{r,echo = F} df [[1]] ` `` 这会生成带有指定图形( df 是一个图表列表,在预览窗格中调用 df [[i]] 生成一个图表)(在RStudio中呈现所有图形内联) p> 我可以使用for循环生成这些标签。 ```{r,results ='asis',echo = FALSE} for(i in 1:length(gg0)){ cat #####,q $ Subheading [i],\\\)} ``` 这产生了所需的输出 - 在子标题列中的名称标签。 然而,我坚持尝试使用for循环来生成图表,这与我手动编写代码时的方式类似。 扩展上面,我尝试生成产生初始输出的降价,但是无法生成(无论是在内联降价还是预览中)。 $ cat(#####,q()){r,results ='asis',echo = FALSE} $ subheading [i],\\\) cat('```{r,echo = F} \ n') cat(gg0 [[,i,] ] \\\) cat('```\\\')} ``` 也许我错过了关于减价的更好的一点?我已经尝试过使用 cat (甚至没有)的各种模式 p 我更喜欢使用RMarkdown解决方案,但其他解决方案是同样欢迎。解决方案我玩了一下,找到了一个解决方案。您必须在 asis 代码块中使用 print ... ```{r} library(ggplot2) gg0 gg0 [[1]]< - ggplot(mtcars,aes(mpg,hp))+ geom_point() gg0 [[2]] gg0 [ [3]] 标题< -c('hp','disp','drat')``` ####标题{.tabset} ```{r,results ='asis',echo = FALSE} for(i in 1:长度(gg0)){ cat(#####,标题[i],\\\) print(gg0 [[i]]) cat('\\\\\\')} ``` I can embed plots using just RMarkdown's {.tabset}#### Heading {.tabset}##### Subheading 1```{r, echo=F}df[[1]]```This produces individual tabs with the specified graphs (df is a list of graphs, calling df[[i]] produces a graph) in the preview pane (renders all the graphs inline in RStudio).And I can generate just the tabs using a for loop.```{r, results='asis', echo = FALSE}for (i in 1:length(gg0)) { cat("##### ",q$Subheading[i],"\n")}```And this produces the desired output - the tabs with the names in the Subheading column.However, I am stuck in trying to generate the graphs themselves using the for loop similar to how I did when I coded it manually.Extending the above, I tried to generate the markdown that produced the initial output but the plot fails to generate (both in the inline markdown and preview).```{r, results='asis', echo = FALSE}for (i in 1:length(gg0)) { cat("##### ",q$Subheading[i],"\n") cat('```{r, echo=F} \n') cat("gg0[[",i,"]]\n") cat('``` \n')}```Maybe I am missing a finer point regarding markdown? I have tried various patterns using cat (and even without)I would prefer a RMarkdown solution but other solutions are just as welcome. 解决方案 I played around a little and found a solution. You have to use print within the asis code chunk...```{r}library(ggplot2)gg0 <- list()gg0[[1]] <- ggplot(mtcars, aes(mpg, hp)) + geom_point()gg0[[2]] <- ggplot(mtcars, aes(mpg, disp)) + geom_point()gg0[[3]] <- ggplot(mtcars, aes(mpg, drat)) + geom_point()headings <- c('hp','disp','drat')```#### Heading {.tabset}```{r, results='asis', echo = FALSE}for (i in 1:length(gg0)) { cat("##### ",headings[i],"\n") print(gg0[[i]]) cat('\n\n')}``` 这篇关于R:你如何以程序的方式将剧情嵌入到RMarkdown的标签中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-31 02:19