[Rd] Problem using callNextMethod() in S4

cstrato cstrato at aon.at
Sun Mar 4 17:26:40 CET 2007


Dear all,

Maybe, I am doing something wrong, but using R-2.5.0 on my Intel-Mac, I 
have problems
using function callNextMethod() in method initialize.

I am loading the following code as file "testS4.R":

setClass("baseClass",
   representation(myname = "character",
                  mydir  = "character",
                  "VIRTUAL"),
   prototype(myname = "",
             mydir  = "")
)#baseClass

setClass("derivedClass",
   representation(mytitle = "character"),
   contains=c("baseClass"),
   prototype(mytitle = "")
)#derivedClass


setMethod("initialize", "baseClass",
   function(.Object, ...) {
print("---initialize:baseClass---")
#    .Object <- callNextMethod();
      if (.Object at mydir == "") {
         .Object at mydir <- as.character(getwd());
      }#if
print(paste("mydir = ", .Object at mydir))
    .Object <- callNextMethod();
    .Object;
   }
)#initialize

setValidity("baseClass",
   function(object) {
print("---setValidity:baseClass---")
      strg <- object at myname;
      if (!(is(strg, "character") && nchar(strg) > 0)) {
         warning(paste(sQuote("myname"), "is missing"));
      }#if
print(paste("myname = ",object at myname))
      strg <- object at mydir;
      if (!(is(strg, "character") && file.exists(strg))) {
         warning(paste(sQuote("mydir"), "is not a system directory"));
      }#if
      if (substr(strg, nchar(strg), nchar(strg)) == "/") {
         object at mydir <- substr(strg, 0, nchar(strg)-1);
      }#if
print(paste("mydir = ",object at mydir))
   }
)#setValidity

setMethod("initialize", "derivedClass",
   function(.Object, ...) {
print("---initialize:derivedClass---")
#    .Object <- callNextMethod();
      if (.Object at mytitle == "") {
         .Object at mytitle = "MyTitle";
      }#if
print(paste("mytitle = ",.Object at mytitle))
#    .Object <- callNextMethod();
    .Object;
   }
)#initialize

setValidity("derivedClass",
   function(object) {
print("---setValidity:derivedClass---")
      strg <- object at mytitle;
      if (!(is(strg, "character") && nchar(strg) > 0)) {
         warning(paste(sQuote("mytitle"), "is missing"));
      }#if
print(paste("mytitle = ",object at mytitle))
   }
)#setValidity


This is the output of an R session:

 > library(methods)
 > source("testS4.R")
 > tmp<-new("derivedClass")
[1] "---initialize:derivedClass---"
[1] "mytitle =  MyTitle"
[1] "---initialize:baseClass---"
[1] "mydir =  /Volumes/CoreData/CRAN/Workspaces/tests"
 >
 > tmp<-new("derivedClass",myname="testname",mytitle="testitle")
[1] "---initialize:derivedClass---"
[1] "mytitle =  MyTitle"
[1] "---initialize:baseClass---"
[1] "mydir =  /Volumes/CoreData/CRAN/Workspaces/tests"
[1] "---setValidity:baseClass---"
[1] "myname =  testname"
[1] "mydir =  /Volumes/CoreData/CRAN/Workspaces/tests"
Error in validObject(.Object) : invalid class "derivedClass" object: 
mydir =  /Volumes/CoreData/CRAN/Workspaces/tests
 >
 > tmp<-new("derivedClass",myname="testname",mydir="",mytitle="testitle")
[1] "---initialize:derivedClass---"
[1] "mytitle =  MyTitle"
[1] "---initialize:baseClass---"
[1] "mydir =  /Volumes/CoreData/CRAN/Workspaces/tests"
[1] "---setValidity:baseClass---"
[1] "myname =  testname"
[1] "mydir =  "
Error in validObject(.Object) : invalid class "derivedClass" object: 
mydir = 
In addition: Warning message:
'mydir' is not a system directory in: validityMethod(as(object, 
superClass))
 >

Can someone tell me why mydir gives an error although it is defined 
correctly?

Thank you in advance
Best regards
Christian
_._._._._._._._._._._._._._._._
C.h.i.s.t.i.a.n S.t.r.a.t.o.w.a
V.i.e.n.n.a       A.u.s.t.r.i.a
_._._._._._._._._._._._._._._._



More information about the R-devel mailing list