2016-01-13 70 views
2

我想將ggplots放入列表,然後打印列表中的每一個。這應該在RStudio中使用rmarkdown和knitr完成。 MWE(當然,在現實中,列表得到填補其他地方比印刷):打印ggplots列表不起作用在knitr與rmarkdown

--- 
title: "nice title" 
author: "Me" 
date: 12\. Januar 2016 
output: 
    pdf_document: 
    fig_caption: yes 
    fig_width: 7 
    keep_tex: yes 
    number_sections: yes 
    toc: yes 
    html_document: default 
--- 

Here is my plot: 

```{r, echo=F} 
library(ggplot2) 
printGGlist <- function(gglist){ 
for(gg in gglist){ 

    print(gg) 

    } 
} 
g1 <- ggplot(diamonds, aes(carat, cut)) + geom_point() 
g2 <- ggplot(diamonds, aes(carat, cut)) + geom_density() 
gg <- list(g1, g2) 
printGGlist(gg) 
``` 
more text 

的LaTeX的代碼,我得到然而

\includegraphics{MyProject_files/figure-latex/unnamed-chunk-7-1.pdf}! 
\href{MyProject_files/figure-latex/unnamed-chunk-7-2.pdf}{}! 
\href{MyProject_files/figure-latex/unnamed-chunk-7-3.pdf}{}! 

結果當然是不漂亮。這裏發生了什麼?

+0

我不知道爲什麼它不爲你工作,但如果用'lapply(gg,function(x)x)'替換'printGGlist(gg)',它就可以正常工作。 – toldo

+0

另外,指定'fig.align ='center''也適用。 –

+0

@toldo:那有效 - 但爲什麼其他的東西不起作用? – Make42

回答

2

這是最近在knitr 1.12介紹了一個錯誤,我已經在開發版本(> = 1.12.2)固定它:https://github.com/yihui/knitr#installation

+0

我想這是用'devtools :: install_github('yihui/knitr')'來安裝的,對吧?如果是這樣,請將其添加到您的帖子,以幫助沒有經驗的讀者;-)。另外:請提及開發版本的穩定性。 – Make42

+0

該鏈接現在將帶您到安裝說明。通常開發版本非常穩定。我不會將不成熟的代碼推向回購協議,並且包中有很多測試。 –