Skip to content
Prev 22700 / 63424 Next

setReplaceMethod

There's a two-level issue here, principle and practice.

The principle is that the behavior of basic R functions on basic R data 
types is considered "sealed".  Math functions, arithmetic, etc. on basic 
vectors are not supposed to be alterable.

The function "[<-" is one of those functions.  If x is a basic vector 
type, then x[i] <- value is considered sealed.  That's what the error 
message is telling you.  (By the way setReplaceMethod() has nothing to 
do with the issue; it's just a way of relieving the programmer from 
knowing that x[i]<-value is really a call to the function "[<-"; again, 
the error message shows that.)

Since you are writing the method for x having class "ANY", that is even 
stronger than changing the behavior for a specific basic type.

If you were to convince the R management that in principle having a 
special class on the right side justified breaking the seal (and that 
would take some arguing), then there is a practical problem.

The current implementation of primitives in C code examines the first 
argument only (except for binary operators, where both arguments are 
examined).  The object is examined for having a non-basic class.  If 
it's a basic data type, no further checking for methods occurs.  So even 
if you were allowed to define the method, it wouldn't get called if x 
was a numeric vector, for example.  Changing that would require changes 
at a low level, and almost certainly add some overhead to all calls.  
Expect opposition.

Otherwise, you must define some class and only expect your assignment to 
promote x if x  inherits from that class.  Or have another function, not 
"[<-", as the generic.

By the way, your method definition is not what you wanted in any case.  
The signature you meant to have was signature(x="ANY", value = "brob").  
At least I assume it was the  right-hand side and not argument "i" that 
you meant to have class "brob".  You don't actually need the x="ANY" 
part, that is the default for omitted arguments--but it's clearer in 
this case to be explicit.
Robin Hankin wrote: