[R] Sweave and the "[" function

Vincent Goulet vincent.goulet at act.ulaval.ca
Tue Sep 5 06:24:00 CEST 2006


Le Mardi 5 Septembre 2006 0:03, hadley wickham a écrit :
> > <<>>=
> > str(women)
> > women$height
> > women[,1]
> > "["(women,1)
> > @
> >
> > to show the equivalence of  three methods of extracting an element from
> > a data.frame.
> >
> > However Sweave returns the last of these as
> >
> > women[1]
> >
> > in the S input chunk
> >
> > How can I force it not to do this and return "["(women,1)
>
> I don't think you can.  Sweave parses your R code and from then on
> uses the internal R representation.  R normalises the parse tree in
> certain ways (eg. strips comments, formats source code, and clearly
> normalises some function calls).  Since sweave uses this, and not the
> original text, I don't think there is anyway to get around this,
> unless there is some trick during parsing.
>
> (And don't forget women[[1]])
>
> Hadley

So here's a workaround (untested):

<<echo=TRUE, eval=TRUE>>=
str(women)
women$height
women[,1]
@
<<echo=TRUE, eval=FALSE>>=
"["(women,1)
@
<<echo=FALSE, eval=TRUE>>=
"["(women,1)
@

I often end up doing similar things.

HTH    Vincent

-- 
  Vincent Goulet, Professeur agrégé
  École d'actuariat
  Université Laval, Québec 
  Vincent.Goulet at act.ulaval.ca   http://vgoulet.act.ulaval.ca



More information about the R-help mailing list