[R] code for [[.data.frame

Romain Francois romain.francois at dbmail.com
Sun Dec 13 19:29:57 CET 2009


On 12/13/2009 02:42 PM, Guillaume Yziquel wrote:
> baptiste auguie a écrit :
>> `[[.data.frame`

Note that this will only be the source code if you have options 
"keep.source" and "keep.source.pkgs" set to TRUE, and the environment 
variable "R_KEEP_PKG_SOURCE" equal to yes.

Otherwise you see the code parsed and deparsed, which loses comments and 
formatting.

 > `[[.data.frame`
function(x, ..., exact=TRUE)
{
     ## use in-line functions to refer to the 1st and 2nd ... arguments
     ## explicitly. Also will check for wrong number or empty args
     na <- nargs() - !missing(exact)
     if(!all(names(sys.call()) %in% c("", "exact")))
         warning("named arguments other than 'exact' are discouraged")

     if(na < 3L)
         (function(x, i, exact)
           if(is.matrix(i)) as.matrix(x)[[i]]
           else .subset2(x, i, exact=exact))(x, ..., exact=exact)
     else {
         col <- .subset2(x, ..2, exact=exact)
         i <- if(is.character(..1))
             pmatch(..1, row.names(x), duplicates.ok = TRUE)
         else ..1
         .subset2(col, i, exact=exact)
     }
}
<environment: namespace:base>


 > writeLines( deparse( `[[.data.frame` ) )
function (x, ..., exact = TRUE)
{
     na <- nargs() - (!missing(exact))
     if (!all(names(sys.call()) %in% c("", "exact")))
         warning("named arguments other than 'exact' are discouraged")
     if (na < 3L)
         (function(x, i, exact) if (is.matrix(i))
             as.matrix(x)[[i]]
         else .subset2(x, i, exact = exact))(x, ..., exact = exact)
     else {
         col <- .subset2(x, ..2, exact = exact)
         i <- if (is.character(..1))
             pmatch(..1, row.names(x), duplicates.ok = TRUE)
         else ..1
         .subset2(col, i, exact = exact)
     }
}

Romain

>> and more generally see Rnews Volume 6/4, October 2006 "Accessing the
>> Sources".
>>
>>
>> HTH,
>>
>> baptiste
>
> Thank you so much.
>
> Indeed, I've been able to look at the source code of the function from
> the source code of R. But I was quite keen on knowing how to do this
> from the toploop. Thanks a lot.
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/HlX9 : new package : bibtex
|- http://tr.im/Gq7i : ohloh
`- http://tr.im/FtUu : new package : highlight




More information about the R-help mailing list