Using child documents with RMarkdown V2

1,549 views
Skip to first unread message

Alan

unread,
Mar 30, 2014, 1:18:49 PM3/30/14
to kn...@googlegroups.com, arnh...@appstate.edu
In the past I have made heavy use of child documents with knitr both with *.Rmd and *.Rnw files.  I am using RMarkdown V2 (RStudio Version 0.98.737) and the 'knit html' in RStudio works as expected.  When I use the new 'knit PDF' in RStudio with a *.Rmd child document that includes graphs....none of the graphs appear in the final pdf.  What am I missing?  Or is this something I should not be attempting...ie use .Rnw if I want *.pdf output?

Here is a basic example:

---
title: "New Stuff!"
author: "Clueless"
date: "March 30, 2014"
output:
  html_document: default
  pdf_document:
    highlight: tango
    keep_tex: yes
  word_document: default
geometry: margin=1in
fontsize: 12pt
---

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r comment = NA}
summary(cars)
```

You can also embed plots, for example:

```{r, echo=FALSE}
plot(cars)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

```{r comment = NA, message = FALSE, echo = FALSE}
library(ggplot2)
ggplot(data = diamonds, aes(x = cut, y = price)) + geom_boxplot() + theme_bw()
```

```{r child='./Children/child1.Rmd'}
```



The child document follows...which is stored in a folder named Children

---
title: "child1"
author: "Clueless"
date: "March 30, 2014"
output: html_document
---

This is child 1.

```{r Rhist, fig.align = "center"}
hist(rnorm(1000), col = "red")
```

Here is some *ggplot2*

```{r GRAPHS, fig.align='center', warning=FALSE,message=FALSE}
library(ggplot2)
ggplot(data = diamonds, aes(x = carat, y = price)) + geom_point(alpha = .2) + theme_bw()
```

The document compiles but does not include graphs (the pdf version)...


TIA for any pointers!

Alan-


The larger problem is I am trying to update my class notes to pdf...using a parent.Rmd that makes several calls to children.Rmd documents...
The notes are on 









Yihui Xie

unread,
Apr 1, 2014, 11:21:47 PM4/1/14
to Alan, knitr
Thanks for the feedback! The problem is not about child documents, but
the chunk option fig.align='center' in your child documents. This
option is not really for the "general-purpose" Markdown, because
Markdown does not support the alignment of images. What knitr does
when it sees fig.align in Markdown is to generate HTML code <img
src=... style="display: block; margin: auto" /> instead of using the
original Markdown syntax ![]() to write images. At the moment, Pandoc
does not seem to recognize the <img> tag when it converts Markdown to
LaTeX.

I think we will solve this problem in the future by using native LaTeX
code to write figures when the document output is expected to be PDF.
For now, the best strategy is to avoid anything that is not supported
in the native (Pandoc) Markdown syntax.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name

Alan

unread,
Apr 3, 2014, 3:19:56 PM4/3/14
to kn...@googlegroups.com, arnh...@appstate.edu
Thanks for the pointer and the great knitr package!

Tao Shi

unread,
Jul 3, 2014, 3:22:35 AM7/3/14
to kn...@googlegroups.com, arnh...@appstate.edu
Hi Yihui,

Just want to follow up on this a little bit.  Any idea how to solve this problem, when generating Word file output?

Thanks!

Tao

Yihui Xie

unread,
Jul 7, 2014, 1:24:17 AM7/7/14
to Tao Shi, knitr, Alan
I think your best bet is to avoid using fig.align.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name


Reply all
Reply to author
Forward
0 new messages