r - How to add multiple figures across multiple pages in a chunk using knitr and RMarkdown? -
r - How to add multiple figures across multiple pages in a chunk using knitr and RMarkdown? -
i using loop create multiple big figures across multiple pages in 1 chunk knitr , rmarkdown. works fine word , html outputs, has 1 problem in pdf output.
this minimal rmarkdown illustration reproduce problem.
--- title: "knitr test" date: "6 apr 2015" output: pdf_document --- ```{r, echo=false, fig.width=6.5,fig.height=10} library(ggplot2) (i in seq(1, 4)){ p <- ggplot(cars, aes(speed, dist)) + geom_point() print(p) } ```
the generated pdf file looks this. 2 figures printed in page.
if alter fig.height, add together few section in rmd file, 2 figures still printed in same page different arrangement.
--- title: "knitr test" output: pdf_document date: "6 apr 2015" --- ## section row b ```{r plot_phenotype, echo = false, fig.height=8, fig.width=6.5} library(ggplot2) (i in seq(1, 4)) { p <- ggplot(cars, aes(speed, dist)) + geom_point() print(p) } ```
thanks suggestion prepare problem.
i using rstudio 0.99.375. session information.
sessioninfo() r version 3.1.3 (2015-03-09) platform: x86_64-w64-mingw32/x64 (64-bit) running under: windows 7 x64 (build 7601) service pack 1 locale: [1] lc_collate=english_australia.1252 lc_ctype=english_australia.1252 [3] lc_monetary=english_australia.1252 lc_numeric=c [5] lc_time=english_australia.1252 attached base of operations packages: [1] stats graphics grdevices utils datasets methods base of operations other attached packages: [1] rmarkdown_0.5.3.1 knitr_1.9.5 loaded via namespace (and not attached): [1] colorspace_1.2-5 digest_0.6.8 evaluate_0.5.5 formatr_1.0 [5] ggplot2_1.0.0 grid_3.1.3 gtable_0.1.2 htmltools_0.2.6 [9] mass_7.3-34 munsell_0.4.2 plyr_1.8.1 proto_0.3-10 [13] rcpp_0.11.5 reshape2_1.4.1 scales_0.2.4 stringr_0.6.2 [17] tcltk_3.1.3 tools_3.1.3 yaml_2.1.13
i have solved problem.
in generated tex file, there aren't new lines after each figure. tex code generated using rmd file above:
\includegraphics{test_files/figure-latex/plot_phenotype-1.pdf} \includegraphics{test_files/figure-latex/plot_phenotype-2.pdf} \includegraphics{test_files/figure-latex/plot_phenotype-3.pdf} \includegraphics{test_files/figure-latex/plot_phenotype-4.pdf}
the solution add together new line after each cycle print figure.
cat('\r\n\r\n')
not sure why need 2 "\r\n" here. generated tex file looks like:
\includegraphics{test_files/figure-latex/plot_phenotype-1.pdf} \includegraphics{test_files/figure-latex/plot_phenotype-2.pdf} \includegraphics{test_files/figure-latex/plot_phenotype-3.pdf} \includegraphics{test_files/figure-latex/plot_phenotype-4.pdf}
this total illustration of rmd file
--- title: "knitr test" output: pdf_document: keep_tex: yes date: "6 apr 2015" --- ## section row b ```{r plot_phenotype, echo = false, fig.height=8, fig.width=6.5} library(ggplot2) library(grid) (i in seq(1, 4)) { grid.newpage() p <- ggplot(cars, aes(speed, dist)) + geom_point() print(p) cat('\r\n\r\n') } ```
r rstudio knitr rmarkdown
Comments
Post a Comment