Re: [Rd] Why cant my S4 class have a slot named `C`?

From: Martin Morgan <mtmorgan_at_fhcrc.org>
Date: Tue, 21 Feb 2012 08:54:21 -0800

On 02/21/2012 08:13 AM, Steve Lianoglou wrote:
> Hi all,
>
> This feature was throwing me for a loop for quite some time until I
> played with the names of the slots.
>
> Consider exhibit A:
>
> ==============
> setClass("SVM",
> representation=representation(
> x='numeric',
> y='numeric',
> C='numeric',
> eps='numeric'),
> prototype=prototype(
> x=numeric(),
> y=numeric(),
> C=numeric(),
> eps=numeric()))
>
> SVM<- function(x, y, C, eps) {
> new("SVM", x=x, y=y, C=C, eps=eps)
> }
>
> And now a call to my "constructor"
>
> R> SVM(1:10, 1:10, 1, 0.5)
> Error in .getClassFromCache(Class, where) :
> Class should be either a character-string name or a class definition

Hi Steve --

args(new) shows that new has a single named argument Class. R's rules of partial matching mean that C matches Class before the unnamed "SVM", as in

   f = function(XXX=1, ...) XXX
   f(2, X=3) # returns 3

so name your arg to new

SVM <- function(x, y, C, eps) {

     new(Class="SVM", x=x, y=y, C=C, eps=eps) }

Martin
>
> ==================
>
> This error occurs with both R-2.14.1 and R-devel
>
>
> Changing the name name from `C` to `Cost` fixes it:
>
> ==================
> setClass("svm",
> representation=representation(
> x='numeric',
> y='numeric',
> Cost='numeric',
> eps='numeric'),
> prototype=prototype(
> x=numeric(),
> y=numeric(),
> Cost=numeric(),
> eps=numeric()))
>
> svm<- function(x, y, Cost, eps) {
> new("svm", x=x, y=y, Cost=Cost, eps=eps)
> }
>
> R> svm(1:10, 1:10, 1, 0.5)
> An object of class "svm"
> Slot "x":
> [1] 1 2 3 4 5 6 7 8 9 10
>
> Slot "y":
> [1] 1 2 3 4 5 6 7 8 9 10
>
> Slot "Cost":
> [1] 1
>
> Slot "eps":
> [1] 0.
>
> ===================
>
> I was fishing around the R-devel mailing list w/ that error message to
> see if anything turned up but I wasn't having any luck so it took a
> while to figure out what was going on.
>
> Is this known/intended behavior? Maybe I missed it in the
> documentation (not sure where that might have been)?
> If it is intended, maybe a better error message would be more useful?
>
> Thanks,
> -steve
>

-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Tue 21 Feb 2012 - 16:56:46 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 Tue 21 Feb 2012 - 18:40:20 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