[Rd] different behavior accessing type-specific as.data.frame inside a function vs inside R shell

From: brian avants <stnava_at_gmail.com>
Date: Wed, 19 Sep 2012 16:06:39 -0400


hello

we are developing an R package called ANTsR for which we have some special types.

one of these types is an " antsMatrix " type.

we implemented a "as.data.frame" function that casts an antsMatrix to a data.frame

this works fine in a basic shell script. for instance:

# install

R CMD INSTALL ANTsR
# open

R
# in R do

library(ANTsR)
a <- new( "antsMatrix", "float" )
b <- as.data.frame( a )

this all works fine and i am pleased.

fyi, the implementation of as.data.frame is:

setMethod( f = "as.data.frame" ,

   signature( x = "antsMatrix" ) ,
   definition = function( x )

          {
  lst = .Call( "antsMatrix_asList" , x )  names(lst)[ 1 : (length(lst)) ] <- lst[[ length(lst) ]] lst[[ length(lst) ]] <- NULL

                        as.data.frame(lst)
}

   )

now the problem comes when i try to access the same functionality in a function that is within my package R source.

i.e. the function is defined in ANTsR/R/test.R which reads

test <- function(...)
{
  a <- new( "antsMatrix", "float" )
  b <- as.data.frame( a )
}

this should produce the same behavior as above, i would think.

but somehow , i get different behavior:

library(ANTsR)
test()

produces :

Error in as.data.frame.default(a) :
  cannot coerce class 'structure("antsMatrix", package = "ANTsR")' into a data.frame

it is clear to me what's happening ----- R is trying to use the default implementation of as.data.frame

which clearly won't work on an antsMatrix type.

the issue is --- i do not know why R is not using the correct type-specific implementation when

this function is called as opposed to when the same operations are called within the shell.

certainly there must be some simple error on our part in implementation but i cannot find

what it is --- i am the 3rd person who has looked into this. it's true that we are all novice R

developers .... am hoping someone on the list can provide some insight.

many thanks for your time,

brian

        [[alternative HTML version deleted]]



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed 19 Sep 2012 - 21:25:01 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 20 Sep 2012 - 07:10:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive