[R] keep track of variables created in each chapter of a knitr book

Michael Friendly friendly at yorku.ca
Fri Jan 24 15:14:39 CET 2014


In a book project using knitr, I'm creating a large number of variable 
and objects in chunks within
chapters.  I'd like to find a way of keeping track of all of those for 
each chapter, and clean up
at the end of each chapter, without having to manually list their names 
as shown below.

The book.Rnw file uses a collection of child documents:

<<ch1, child='ch01.Rnw'>>=
@

<<ch2, child='ch02.Rnw'>>=
@

<<ch3, child='ch03.Rnw'>>=
@
...

A typical chapter file, ch02.Rnw begins with a setup chunk and ends with 
a cleanup chunk:

<<setup2, echo=FALSE>>=
source("Rprofile.R")
knitrSet("ch02")
require(vcdExtra, quietly = TRUE, warn.conflicts = FALSE)
@

.... content ...

<<cleanup2,results='hide'>>=
remove(list=objects(pattern="array|mat|my|\\.tab|\\.df"))
remove(list=c("A", "B", "age", "count", "ds", "n", "passed", "sex", 
"tab", "tv.data", "TV2", "TV"))
ls()
@

-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA



More information about the R-help mailing list