Skip to content

Sweave: \Sexpr{} inside <<>>?

5 messages · zerfetzen, Dieter Menne, Duncan Murdoch

#
Hi,
Is it possible in Sweave to put \Sexpr{} inside <<>>?  This is a bad
example, but here goes:

<<results=hide>>
Age <- 5
@

<<>>
x <- \Sexpr{Age}
@

I'm trying to get it to display x <- 5, rather than x <- Age.  It's probably
so obvious I'm going to feel sorry for having to ask, just the same, I'm
stumped.  Any ideas?  Thanks.
#
zerfetzen wrote:
It's good to have a simplified example, but your's is so much simplified
that it is difficult to understand. I know I had similar hen-and-egg
problems before, so 

http://r-project.markmail.org/thread/xaflfjwt65dkgphi

might guide you. If not, repost and make it clearer what the problem is.

Dieter
#
On 11-01-25 8:22 PM, zerfetzen wrote:
>
 > Hi,
 > Is it possible in Sweave to put \Sexpr{} inside<<>>?  This is a bad
 > example, but here goes:
 >
 > <<results=hide>>
 > Age<- 5
 > @
 >
 > <<>>
 > x<- \Sexpr{Age}
 > @
 >
 > I'm trying to get it to display x<- 5, rather than x<- Age.  It's 
probably
 > so obvious I'm going to feel sorry for having to ask, just the same, I'm
 > stumped.  Any ideas?  Thanks.

No, you can't do that.  There are a couple of ways to do what you want. 
  Probably the easiest is to do what Sweave would do:

<<results=hide>>=
Age<- 5
@

\begin{Schunk}
\begin{Sinput}
 > x<- \Sexpr{Age}
\end{Sinput}
\end{Schunk}

Duncan Murdoch
#
Thanks Duncan, that helps.  It successfully displays what I'm looking for,
but it is not executing it.  In a previous code chunk, it notes the time it
took to run something, and in the successive code chunk, it runs something
else where the previous time is now a parameter, but I'd like it to
numerically display that previous time in the new chunk, rather than a
variable name I create for it behind the scenes.  Is this possible?  Many
thanks.
#
On 11-01-26 3:43 PM, zerfetzen wrote:
Of course it's possible, but it'll need ugly trickery.  Just do every 
calculation in a hidden chunk, then use code like I did to fake a 
display of it.

Duncan Murdoch