如何在rmakdown中通过乳胶循环? [英] how to loop through in rmakdown with latex?

查看:68
本文介绍了如何在rmakdown中通过乳胶循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想结合使用rmarkdown和Latex并遍历数据集列.有什么方法可以处理下面的代码

I want to combine rmarkdown and latex and loop through a dataset column. Is there any way to work on code below

```{r}

for (i in 1:10){
  
\begin{itemize}

\item i

\end{itemize}

}
```

推荐答案

一种解决方法

```{r, echo=FALSE, results='asis'}
dfex <- data.frame(x=letters[1:10], y=LETTERS[1:10])

cat("\\begin{itemize}")
for(i in 1:nrow(dfex)){
  cat(paste("\\item", dfex[i,1]))  
}
cat("\\end{itemize}")
```

重要说明: results ='asis' cat 确保将其生成为干净的乳胶输出而不是R输出.需要双 \\ 来明确声明它是我们想要的反斜杠,而不是某些特殊字符,例如 \ i .

Important to note: results='asis' and cat makes sure it is generated as a clean latex output rather than R output. Need double \\ to explicitly state that it is backslash we want rather than some special character e.g. \i.

无需使用( knitr :: asis_output )

```{r, echo=FALSE}
latexItemize <- function(items){
  knitr::asis_output(paste0(
    "\\begin{itemize}",
    paste("\\item", items, collapse=" "),
    "\\end{itemize}"
  ))

}
```


```{r, echo=FALSE}
latexItemize(dfex[,2])
```

这篇关于如何在rmakdown中通过乳胶循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆