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

Martin Morgan mtmorgan at fhcrc.org
Tue Feb 21 17:54:21 CET 2012


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



More information about the R-devel mailing list