r - Defer code to END of document in knitr -
i trying write report in rmarkdown , use knitr generate pdf.
i want code pushed "end of document", while displaying results interweaved text. echo='hold' option doesn't this.
section of markdown file
generate data ```{r chunk1,echo='hold',r.options=} num_seq<-rnorm(100,0.2) num_seq ``` further report mean of these numbers. ```{r,echo='hold' } mean(num_seq) ``` i have tried read the relevant documentation found here http://yihui.name/knitr/options/, can't figure out how this.
i don't think echo='hold' option. regardless, trick use echo=false code included, , re-use same chunk name , use eval=false want code printed. (other options in both locations fine, these 2 minimum required.)
the following evaluates code (and optionally includes output it) chunk located, doesn't include code until specify.
# header 1 ```{r chunk1, echo=false} x <- 1 x + 5 ``` test. ```{r chunk1, eval=false} ``` results in following markdown:
header 1 ======== ## [1] 6 test. x <- 1 x + 5 edit: use in r markdown documents randomness: store random seed in beginning (whether set manually or store current random state later reproduction) , display in annex/appendix:
# header 1 ```{r setseed, echo=false, include=false} set.seed(seed <- sample(.machine$integer.max, size=1)) seed ``` test `r seed`. # annex {-} ```{r showsetseed, ref.label='setseed', eval=false} ``` ```{r printseed, echo=false} seed ``` this example doesn't include results original code chunk. unfortunately, results aren't stored, , if set eval=true when use same chunk name later, calculate , present different seed. that's why printseed block. reason explicitly "show" seed in first setseed block solely that, in annex, showsetseed , printseed chunks flow well. (otherwise, set.seed not return number, have looked wierd.)
btw: second example uses ref.label, yihui documents here more general approach chunk reuse.
btw #2: when said "store random state", that's not correct ... i'm storing randomly-generated seed. random state larger single integer, of course. don't want anger prng gods :-)
Comments
Post a Comment