[Rd] Re: [Omega-bugs] RSMethods pkg causes data.frame misbehavior in R

John Chambers jmc@research.bell-labs.com
Fri, 30 Mar 2001 10:20:05 -0500 (EST)


Right.  The methods such as as.data.frame.numeric don't get
dispatched, basically because RSMethods tries to interpret class(x) in
the S4 sense (so, e.g. a numeric vector has class "numeric").

Problems can then come from different semantics for S3-style and
S4-style methods.  The older methods don't dispatch on the mode of
vectors (or equivalently, on the value of data.class(x)), but only on
an explicit "class" attribute.

In order to have separate methods for as.data.frame.numeric, etc.,
the S3-style generic function explicitly assigns a class (attribute).

In the newer class model, every object has a class so no explicit
attribute is needed.

The long-term solution depends on how we want to approach the two
kinds of methods:  Do we want them to live side-by-side or do we want
a single method dispatch model?

Meanwhile, a work-around is to find places, like as.data.frame, where
the class attribute is set, and make this explicit.

This specific bug goes away if the RSMethods package overrides
as.data.frame with the following definition.  (I'll add it to the next
version of RSMethods if it proves OK.)

as.data.frame <-
function (x, row.names = NULL, optional = FALSE) 
{
    if (is.null(x)) 
        return(as.data.frame(list()))
    if (is.null(attr(x, "class"))) 
        attr(x, "class") <- data.class(x)
    UseMethod("as.data.frame", x, row.names, optional)
}
  
Like most of these problems, I suspect, all that's needed is to
replace "class(x)" with "attr(x, 'class')".

Thanks, Vince.

John

-- 
John M. Chambers                  jmc@bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc

> From: "Vincent J. Carey, Jr." <stvjc@channing.harvard.edu>
> Sender: omega-bugs-admin@omegahat.org
> Errors-To: omega-bugs-admin@omegahat.org
> Precedence: bulk
> List-Help: <mailto:omega-bugs-request@omegahat.org?subject=help>
> List-Post: <mailto:omega-bugs@omegahat.org>
> List-Subscribe: <http://www.omegahat.org/mailman/listinfo/omega-bugs>,
> 	<mailto:omega-bugs-request@omegahat.org?subject=subscribe>
> List-Id: Bug reports and related mail for Omegahat software. <omega-bugs.omegahat.org>
> List-Unsubscribe: <http://www.omegahat.org/mailman/listinfo/omega-bugs>,
> 	<mailto:omega-bugs-request@omegahat.org?subject=unsubscribe>
> List-Archive: <http://www.omegahat.org/pipermail/omega-bugs/>
> Date: Thu, 29 Mar 2001 18:10:49 -0500 (EST)
> 
> 
> I've run into a problem with data.frame printing/attachment
> in R1.3 (also detected in 1.2) when RSMethods is attached as a library.
> Here are the versions I'm using, they are the latest ones pointed to
> on omega.stat.wisc.edu:
> 
> -rw-r--r--   1 stvjc    st         16920 Mar  5 13:38 SLanguage_0.1-0.tar.gz
> -rw-r--r--   1 stvjc    st         38139 Mar  5 13:37 RSMethods_0.1-2.tar.gz
> 
> A transcript displaying the problem and a traceback is given
> below.  When RSMethods is detached the problem goes away.
> I haven't been able to understand why as.data.frame.default
> kicks in erroneously.
> 
> I'm still able to make progress with the S4 classes in R
> with the RSMethods library, so I don't consider this too 
> urgent.
> 
> vc
> 
> # normal operations prior to library attachment
> 
> > data(stackloss)
> > stackloss[1:3,]
>   Air.Flow Water.Temp Acid.Conc. stack.loss
> 1       80         27         89         42
> 2       80         27         88         37
> 3       75         25         90         37
> 
> # attach
> 
> > library(RSMethods)
> 
> Attaching Package "package:RSMethods":
> 
> 
> 	The following object(s) are masked from package:base :
> 
> 	 all.names all.vars assign exists find get objects remove substring substring<- warning 
> 
> # problem
> 
> > stackloss[1:3,]
> Error in as.data.frame.default(x[[i]], optional = TRUE) : 
> 	can't coerce character into a data.frame
> Execution halted
> 
> > traceback()
> 11: stop(paste("can't coerce", data.class(x), "into a data.frame"))
> 10: as.data.frame.default(x[[i]], optional = TRUE)
> 9: as.data.frame(x[[i]], optional = TRUE)
> 8: data.frame(Air.Flow = c("80", "80", "75"), Water.Temp = c("27", 
>    "27", "25"), Acid.Conc. = c("89", "88", "90"), stack.loss = c("42", 
>    "37", "37"), check.names = FALSE, row.names = c("1", "2", "3"
>    ))
> 7: do.call("data.frame", rval)
> 6: format.data.frame(x)
> 5: format(x)
> 4: as.matrix(x)
> 3: print.matrix(format(x), ..., quote = quote, right = right)
> 2: print.data.frame(structure(list(Air.Flow = c(80, 80, 75), Water.Temp = c(27, 
>    27, 25), Acid.Conc. = c(89, 88, 90), stack.loss = c(42, 37, 37
>    )), .Names = c("Air.Flow", "Water.Temp", "Acid.Conc.", "stack.loss"
>    ), row.names = c("1", "2", "3"), class = "data.frame"))
> 1: print(structure(list(Air.Flow = c(80, 80, 75), Water.Temp = c(27, 
>    27, 25), Acid.Conc. = c(89, 88, 90), stack.loss = c(42, 37, 37
>    )), .Names = c("Air.Flow", "Water.Temp", "Acid.Conc.", "stack.loss"
>    ), row.names = c("1", "2", "3"), class = "data.frame"))
> 
> --
> 
> # also occurs when just running data()
> 
> > search()
> [1] ".GlobalEnv"        "package:SLanguage" "package:RSMethods"
> [4] "package:ctest"     "Autoloads"         "package:base"     
> > data(stackloss)
> Error in as.data.frame.default(x[[i]], optional = TRUE) : 
>         can't coerce numeric into a data.frame
> > detach(3)
> > data(stackloss)
> # no error now
> > search()
> [1] ".GlobalEnv"        "package:SLanguage" "package:ctest"    
> [4] "Autoloads"         "package:base"     
> 
> 
> > 
> > version
>          _                           
> platform sparc-sun-solaris2.6        
> arch     sparc                       
> os       solaris2.6                  
> system   sparc, solaris2.6           
> status   Under development (unstable)
> major    1                           
> minor    3.0                         
> year     2001                        
> month    03                          
> day      04                          
> language R                           
> 
> i've had the same behavior under R1.3 on RH linux 6.1
> and in R1.2b on that OS
> -- 
> stvjc@gauss.bwh.harvard.edu
> _______________________________________________
> Omega-bugs mailing list  -  Omega-bugs@omegahat.org
> http://www.omegahat.org/mailman/listinfo/omega-bugs
> 
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._