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

Ista Zahn istazahn at gmail.com
Fri Jan 24 18:59:00 CET 2014


I'm not sure what exactly you want to remove in the cleanup step, but
to list the objects assigned in a particular chapter can't you just
put

prev.vars <- ls()

at the beginning of the chapter and

vars.this.chapter <- setdiff(ls(), prev.vars)

at the end?

Best,
Ista

On Fri, Jan 24, 2014 at 9:14 AM, Michael Friendly <friendly at yorku.ca> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list