File: standardMethods.R

package info (click to toggle)
r-cran-distr 2.9.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,344 kB
  • sloc: ansic: 199; sh: 13; makefile: 2
file content (99 lines) | stat: -rw-r--r-- 3,251 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
################################################################################

## standardMethods

## generates standard methods for an existing class

## args:
##
## class         -    string containing the class names
## writetofile   -    logical value: TRUE, if output is to be written on file
## directory     -    target directory for the file to be created

## value:
##
## none

## Details:
##
## If output, too, is to be written to a file, standardMethods generates a file
## <classname>_StandardMethods.txt in the given directory

################################################################################

standardMethods <- function(class, writetofile = FALSE, directory){
  ClassRep <- getClassDef(class)
  SlotNames <- names(getSlots(ClassRep))
  nrSlots <- length(SlotNames)
  
  if(!nrSlots) return()
  
  part0 <- "if(!isGeneric(\""
  part1 <- "\")) setGeneric(\""
  part2 <- "\", function(object) standardGeneric(\""
  part3 <- "\"))"
  
  for(i in 1:nrSlots){
    string <- paste(part0, SlotNames[i], part1, SlotNames[i],
                    part2, SlotNames[i], part3, "\n", sep = "")
    if(writetofile) cat(string, 
                        file = paste(directory, class, 
                                     "_StandardMethods.txt", sep=""), 
                        append = FALSE)
    cat(string, sep = "")
  }    
  
  part1 <- "setMethod(\""
  part2 <- "\", \""
  part3 <- "\", function(object) object@"
  part4 <- ")"
  
  for(i in 1:nrSlots){
    string <- paste(part1, SlotNames[i], part2, class, 
                    part3, SlotNames[i], part4, "\n", sep = "")
    if(writetofile) cat(string, 
                        file = paste(directory, class, 
                                     "_StandardMethods.txt", sep=""), 
                        append = TRUE)
    cat(string, sep = "")
  }    
  
  part0 <- "if(!isGeneric(\""
  part1 <- "<-\")) setGeneric(\""
  part2 <- "<-\", function(object, value) standardGeneric(\""
  part3 <- "<-\"))"
  
  for(i in 1:nrSlots){
    string <- paste(part0, SlotNames[i], part1, SlotNames[i], 
                    part2, SlotNames[i], part3, "\n", sep = "")
    if(writetofile) cat(string, 
                        file = paste(directory, class, 
                                     "_StandardMethods.txt", sep=""), 
                        append = TRUE)
    cat(string, sep = "")
  }    
  
  part1 <- "setReplaceMethod(\""
  part2 <- "\", \""
  part3 <- "\", function(object, value){ object@"
  part4 <- " <- value; object})"
  
  for(i in 1:nrSlots){
    string <- paste(part1, SlotNames[i], part2, class, 
                    part3, SlotNames[i], part4, "\n", sep = "")
    if(writetofile) cat(string, 
                        file = paste(directory, class, 
                                     "_StandardMethods.txt", sep=""), 
                        append = TRUE)
    cat(string, sep = "")
  }    
}            



## Example

##setClass("testclass", representation(a = "numeric", b = "character"))
##standardMethods("testclass")
##directory = "C:/TMP/"  ## directory C:\TMP must exist ...
##standardMethods("testclass", writetofile = TRUE, directory = directory)