yihui / knitr-examples

A collection of knitr examples
484 stars 587 forks source link

example 020 for loop in markdown #11

Closed hugokoopmans closed 11 years ago

hugokoopmans commented 11 years ago

Hi,

Tx for your great work, I have been using knitr with latex for a while now I am switching to markdown. The issue I have is with child docś and child_knitr.

I have a R script that reads data on which I want to do automatic processing (e.g. determine if varabels are numeric or categoric and make a plot or stats based on type of variable in dataset) fully automatic. In the example below I have a list of numeric variable names and for each name in the dataset I want to make a plot and print some stats.

Like your example 020 for loop but then in markdown instead of sweave

Now with Latex the below chunk works but I cannot seem to get it running using markdown.

Please advise

## @knitr run-numeric-lx
out = NULL
for (i in c(1:num_vars)) {
  out = c(out, knit_child('da-numeric-template.Rnw', sprintf('da-numeric-template-%d.txt', i)))
}

## @knitr run-numeric-md
out = NULL
for (i in c(1:num_vars)) {
  out = c(out, knit_child('da-numeric.Rmd',sprintf('da-numeric-template-%d.txt', i)))
}

tx

hugo

yihui commented 11 years ago

Please take a closer look at that example. In particular, I only have one argument in knit_child(). Note you have to use the latest version of knitr as well.

hugokoopmans commented 11 years ago

Hi Yihui, I did take a closer look and found the following: Without externalizig the R code the md example works ok.

I can run the example and gives me expected results in markdown.

But if i externalize te R code and read it with 'read_chunk' results are like:

Collect results from the template for each i and write them back later.

read_chunk("020-for-loop-run-all.R")
\section{Now i is \Sexpr{i}}

<<>>= print(i) iris[i, ] @

\section{Now i is \Sexpr{i}}

<<>>= print(i) iris[i, ] @

\section{Now i is \Sexpr{i}}

<<>>= print(i) iris[i, ] @

These seem Latex like to me? Does knitr_child do latex by default?

Please advise.

yihui commented 11 years ago

No, knit_child() simply follows the syntax of the parent document. It does not use LaTeX by default.

If you want to use Markdown, you should use Markdown for both the parent document and the template. You are not supposed to use the Rnw/LaTeX syntax <<>>=.

I have no idea about your 020-for-loop-run-all.R. Please post a complete example if possible.

hugokoopmans commented 11 years ago

Hi Yihui,

thank you for your time, indeed the issue was that in the externalized R script I forgot to replace the Rnw with the Rmd. Now I added two chunks one for latex and one for markdown and all works well. Sorry for my sloppyness, I should have seen this before posting.

hugo

## @knitr run-all-lx
out = NULL
for (i in 1:3) {
  out = c(out, knit_child('020-for-template.Rnw'))
}

## @knitr run-all-md
out = NULL
for (i in 1:3) {
  out = c(out, knit_child('020-for-template.Rmd'))
}
yihui commented 11 years ago

No problem.