r - How to force Knitr to evaluate \Sexpr after all other code chunks -


i trying write abstract dynamic document, \sexpr{} calls not working.

essentially trying start document off abstract has p-values generated \sexpr{value} value determined "downstream" in document. example

this works:

\begin{document}  <<foo>>=    value = 10 @  today bought \sexpr{value} salamanders  \end{document} 

this not work (and trying accomplish)

\begin{document}  today bought \sexpr{value} salamanders  <<foo>>=   value = 10 @ 

i don't see straightforward solution postpone evaluation of \sexpr after evaluation of code chunks, still easy use \sexp values defined later in, example, abstract: use separate file (myabstract.rnw) abstract, add \input{myabstract} abstract supposed included , knit myabstract.rnw @ end of main document:

document.rnw:

\documentclass{article} \begin{document}  \begin{abstract}   \input{myabstract} \end{abstract}  main text.  <<>>= answer <- 42 @  \end{document}  <<include = false>>= knit("myabstract.rnw") @ 

myabstract.rnw:

the answer \sexpr{answer}. 

key understanding how works realize knitr processes document before latex does. therefore, doesn't matter latex command \input{myabstract} includes myabstract.tex "before" (not referring time referring line number), knit("myabstract.rnw") generates myabstract.tex.


for more complex scenarios, evaluation , output separated: calculations in chunks , print results belong. show source code, reuse chunks (setting eval = false). using example above, means:

\documentclass{article} \begin{document}  <<calculation, include = false>>= answer <- 42 @  \begin{abstract}   answer \sexpr{answer}. \end{abstract}  main text.  <<calculation, eval = false>>= @  \end{document} 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -