Re: [R] Sweave and the "[" function

From: Ross Darnell <>
Date: Tue 05 Sep 2006 - 10:56:33 GMT

Hi Vincent

This would seem logical but in this case doesn't work. It doesn't seem to be a Sweave problem (feature) at all but within R as Hadley stated.
Within R try

 > quote(women[1])

now try

 > quote("["(women,1))

So it's parsed and "normalised" (there's a familiar term); in this case to women[1] before its quoted.

Curiously in the "R Language Definition Guide" in 10.4.3 it states

R has three indexing constructs, two of which are syntactically similar although with somewhat
different semantics:
object [ arg1, ...... , argn ]
object [[ arg1, ...... , argn ]]
The object can formally be any valid expression, but it is understood to
denote or evaluate
to a subsettable object. The arguments generally evaluate to numerical or character indices,
but other kinds of arguments are possible (notably drop = FALSE). Internally, these index constructs are stored as function calls with function name "[" respectively

So I'm lost now. Can some one hand me a map and compass?  

Vincent Goulet wrote:

> 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 > > ______________________________________________ mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue Sep 05 21:07:30 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 07 Sep 2006 - 07:51:18 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.