Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed knit_child when knitting chunks with blocks that produce multiple ... #831

Merged
merged 1 commit into from
Sep 1, 2014
Merged

fixed knit_child when knitting chunks with blocks that produce multiple ... #831

merged 1 commit into from
Sep 1, 2014

Conversation

kforner
Copy link
Contributor

@kforner kforner commented Aug 19, 2014

fixed knit_child when knitting chunks with blocks that produce multiple plots.
cf #824

It was difficult writing a test because I could not manage to run your testit tests without running R CMD check, and you do not seem to use sample files in your tests (is there any reason not to use testthat ?)

To test it manually:

library(devtools)
load_all('knitr')
res <- knit('parent.Rnw', quiet = TRUE)
lines <- readLines(res)
nb1 <- sum(grepl('includegraphics', lines, ignore.case = TRUE))
cat('nb plots=', nb1, '\n')

with
parent.Rnw

\documentclass[letter, margin=1.90cm, 12pt]{scrartcl}
\begin{document}

<<parent_call_child, message = FALSE, include = FALSE, results = 'asis', eval = TRUE>>=
library(ggplot2)
out <- knitr::knit_child('child.Rnw', options = list(fig.keep='all'))
@
\Sexpr{paste(out, collapse="\n")}
\end{document}

child.Rnw

\section{PLOTS}
<<plots_for_loop, message = FALSE, echo = FALSE, results = 'asis', fig.keep = 'all', fig.show = 'asis', eval = TRUE>>=
{
  print(ggplot(iris, aes(x = Species)) + geom_histogram(fill = 'green') + ggtitle(paste('PLOT CHILD 1')))
  print(ggplot(iris, aes(x = Species)) + geom_histogram(fill = 'green') + ggtitle(paste('PLOT CHILD 2')))
}
@

The old knitr would output 1, the fixed one 2.

@kforner
Copy link
Contributor Author

kforner commented Aug 27, 2014

Hi. Any chance that this fix gets merged into master soon ?

@yihui yihui added this to the v1.7 milestone Sep 1, 2014
@yihui yihui added the Bug label Sep 1, 2014
yihui added a commit that referenced this pull request Sep 1, 2014
fixes #824: store all hooks before knitting a child document, and restore them after the child document is finished
@yihui yihui merged commit 9073d08 into yihui:master Sep 1, 2014
@yihui
Copy link
Owner

yihui commented Sep 1, 2014

Merged. Thanks!

Regarding the tests, you can just run test-all.R, e.g.

cd tests
Rscript run-all.R

or in R:

setwd('tests')
source('run-all.R')

I do not use testthat because I'm not interested in its new vocabulary (I'm familiar with R's native vocabulary such as identical() and all.equal(), and I do not want to memorize what expect_identical() and expect_equal() exactly mean). More here: http://yihui.name/en/2013/09/testing-r-packages/

@yihui
Copy link
Owner

yihui commented Sep 1, 2014

Well, this is not really a safe fix. The existing hooks should not be set to NULL. We should only remove the previous hooks added by the previous evaluate() call. I'll fix it.

yihui added a commit that referenced this pull request Sep 1, 2014
@kforner
Copy link
Contributor Author

kforner commented Sep 1, 2014

We should only remove the previous hooks added by the previous evaluate() call

You mean that for each hook, you will remove the last function, assuming that it has been added by evaluate ?
What about multiple levels of evaluate recursion ? We are actually using at least 3 levels.

@yihui
Copy link
Owner

yihui commented Sep 1, 2014

Yes. It can be complicated. I have fixed the problem in the evaluate package.

@kforner
Copy link
Contributor Author

kforner commented Sep 2, 2014

Yes and indeed it seems to work pretty well. thanks !!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants