我正在尝试渲染一个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
答案 0 :(得分:1)
正如CL的评论中所建议的那样。和JasonAizkalns,编织错误就是问题所在,所以下载knitr的开发版应该可以解决这个问题。
update.packages(ask = FALSE, repos = 'http://cran.rstudio.org')
install.packages('knitr', repos = c('http://yihui.name/xran', 'http://cran.rstudio.org'))
OR
devtools::install_github('yihui/knitr', build_vignettes = TRUE)
对于遇到同样问题的其他人,在打印命令为错误的解决方法后,无法/不愿意下载开发版本添加cat('\ n \ n')!
即
for(v in Values){
print(ggplot(mtcars, aes(x=carb, y = v, shape=gear)) + geom_point())
cat('\n\n')
}