[英]plots not being printed in for loop when rendering PDF (since ggplot2 update)
我試圖渲染一個PDF,其中循環輸出圖。 我之前使用的代碼按預期工作,但現在只有循環中的第一個圖正在pdf中打印。 問題可能與ggplot2更新有關(雖然我不確定)。
下面我提供了一個可重現的例子(因為我的rmarkdown文件太長了),它執行相同的錯誤 - 只打印循環中的第一個圖。
當然,我可能會遺漏一些完全明顯的東西! 任何幫助非常感謝。
---
title: "Check"
output: pdf_document
---
```{r, results='asis'}
library(ggplot2)
Values = names(mtcars)[1:4]
mtcars$gear <- as.factor(mtcars$gear)
for(v in Values){
print(ggplot(mtcars, aes(x=carb, y = v, shape=gear)) + geom_point())
}
```
> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_Ireland.1252 LC_CTYPE=English_Ireland.1252 LC_MONETARY=English_Ireland.1252 LC_NUMERIC=C
[5] LC_TIME=English_Ireland.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] HH_3.1-24 gridExtra_2.0.0 multcomp_1.4-1 TH.data_1.0-6 survival_2.38-3 mvtnorm_1.0-3 latticeExtra_0.6-26
[8] RColorBrewer_1.1-2 lattice_0.20-33 stringr_1.0.0 pander_0.6.0 xtable_1.8-0 ggplot2_2.0.0 rmarkdown_0.9.2
[15] markdown_0.7.7 knitr_1.12
loaded via a namespace (and not attached):
[1] zoo_1.7-12 reshape2_1.4.1 splines_3.2.3 colorspace_1.2-6 htmltools_0.3 yaml_2.1.13 gmp_0.5-12 foreign_0.8-66
[9] Rmpfr_0.6-0 plyr_1.8.3 munsell_0.4.2 gtable_0.1.2 leaps_2.9 codetools_0.2-14 evaluate_0.8 labeling_0.3
[17] httpuv_1.3.3 lmtest_0.9-34 vcd_1.4-1 proto_0.3-10 Rcpp_0.12.0 acepack_1.3-3.3 scales_0.3.0 formatR_1.2.1
[25] Hmisc_3.16-0 abind_1.4-3 mime_0.4 digest_0.6.9 stringi_1.0-1 shiny_0.13.0 tools_3.2.3 sandwich_2.3-3
[33] magrittr_1.5 Formula_1.2-1 cluster_2.0.3 MASS_7.3-45 R6_2.1.0 rpart_4.1-10 nnet_7.3-11 R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_Ireland.1252 LC_CTYPE=English_Ireland.1252 LC_MONETARY=English_Ireland.1252 LC_NUMERIC=C
[5] LC_TIME=English_Ireland.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] HH_3.1-24 gridExtra_2.0.0 multcomp_1.4-1 TH.data_1.0-6 survival_2.38-3 mvtnorm_1.0-3 latticeExtra_0.6-26
[8] RColorBrewer_1.1-2 lattice_0.20-33 stringr_1.0.0 pander_0.6.0 xtable_1.8-0 ggplot2_2.0.0 rmarkdown_0.9.2
[15] markdown_0.7.7 knitr_1.12
loaded via a namespace (and not attached):
[1] zoo_1.7-12 reshape2_1.4.1 splines_3.2.3 colorspace_1.2-6 htmltools_0.3 yaml_2.1.13 gmp_0.5-12 foreign_0.8-66
[9] Rmpfr_0.6-0 plyr_1.8.3 munsell_0.4.2 gtable_0.1.2 leaps_2.9 codetools_0.2-14 evaluate_0.8 labeling_0.3
[17] httpuv_1.3.3 lmtest_0.9-34 vcd_1.4-1 proto_0.3-10 Rcpp_0.12.0 acepack_1.3-3.3 scales_0.3.0 formatR_1.2.1
[25] Hmisc_3.16-0 abind_1.4-3 mime_0.4 digest_0.6.9 stringi_1.0-1 shiny_0.13.0 tools_3.2.3 sandwich_2.3-3
[33] magrittr_1.5 Formula_1.2-1 cluster_2.0.3 MASS_7.3-45 R6_2.1.0 rpart_4.1-10 nnet_7.3-11
正如CL的評論中所建議的那樣。 和JasonAizkalns,一個knitr bug是問題所在,所以下載knitr的開發版本應該解決這個問題。
update.packages(ask = FALSE, repos = 'http://cran.rstudio.org')
install.packages('knitr', repos = c('http://yihui.name/xran', 'http://cran.rstudio.org'))
要么
devtools::install_github('yihui/knitr', build_vignettes = TRUE)
對於面臨同樣問題的任何其他人,在打印命令為bug的變通方法之后,無法/不願意下載開發版本添加cat('\\ n \\ n')!
即
for(v in Values){
print(ggplot(mtcars, aes(x=carb, y = v, shape=gear)) + geom_point())
cat('\n\n')
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.