# [R] Sweave and the "[" function

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Sep 5 13:46:06 CEST 2006

```Ross Darnell <r.darnell at uq.edu.au> writes:

> 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
> Within R try
>
>  > quote(women[1])
> women[1]
>
> now try
>
>  > quote("["(women,1))
> 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
> "[[".

Clear enough to me, but then again, I probably wrote it... 5 years ago
or so.

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

Er, maybe, if you tell us which woods you are lost in so that we can
find you...

What may be confusing you is that it is precisely the other way
around: women[1] is parsed to "["(women,1) which deparses to women[1].

This may be illuminating:

for(i in 1:3) print(quote(foo(women,1))[[i]])
for(i in 1:3) print(quote("["(women,1))[[i]])
for(i in 1:3) print(quote(women[i])[[i]])

--
O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907

```