[R] keep track of variables created in each chapter of a knitr book
wdunlap at tibco.com
Sun Jan 26 00:56:22 CET 2014
You could also use a new environment for each chapter
e1 <- new.env()
with(e1, A <- c(1,2,3)) # or, if you prefer, e1$A <- c(1,2,3)
However, you may want to also reload packages at the start
of each chapter (I think this makes it more reader-friendly),
so perhaps you should restart R for each chapter.
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Rainer Schuermann
> Sent: Saturday, January 25, 2014 9:39 AM
> To: r-help at r-project.org
> Subject: Re: [R] keep track of variables created in each chapter of a knitr book
> You could initialize one list per chapter,
> > x1 <- list( "Chapter One" )
> and then crate your variables as list members
> > x1$A <- c( 1, 2, 3 )
> > x1$B <- "bla"
> > x1$tv.data <- data.frame( m = sample( LETTERS, 5 ),
> n = round( runif( 5 ), 2 ) )
> > x1
>  "Chapter One"
>  1 2 3
>  "bla"
> n m
> 1 T 0.92
> 2 G 0.77
> 3 B 0.96
> 4 W 0.67
> 5 S 0.16
> which you can keep track of easily at any time. You could save each list per chapter, if
> you wanted to. And you can remove it as easily with a simple
> > rm (x1 )
> A bit more typing, but much cleaner, I think.
> On Friday 24 January 2014 09:14:39 Michael Friendly 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()
> > @
> R-help at r-project.org mailing list
> 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