Skip to content

Commit 540450b

Browse files
committed
add library slot and initialize
checks for presence of required libraries listed in libraries slot moved initialise from param_class to struct_class
1 parent 8c28cd1 commit 540450b

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

R/parameter_class.R

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,6 @@ parameter_class<-setClass(
3232
"parameter_class"
3333
)
3434

35-
## initialise parameters on object creation
36-
setMethod(f="initialize",
37-
signature="parameter_class",
38-
definition=function(.Object,...)
39-
{
40-
L=list(...)
41-
SN=slotNames(.Object)
42-
if (length(L)>0)
43-
{
44-
for (i in seq_len(length(L)))
45-
{
46-
if (names(L)[[i]] %in% SN) {
47-
slot(.Object,names(L)[[i]])=L[[names(L)[[i]]]]
48-
} else {
49-
param.value(.Object,names(L)[[i]])=L[[names(L)[[i]]]]
50-
}
51-
}
52-
}
53-
return(.Object)
54-
}
55-
)
56-
5735
#' @describeIn parameter_class a parameter as an object (if appropriate)
5836
#' @export
5937
setMethod(f="param.obj",

R/struct_class.R

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,49 @@ struct_class<-setClass(
1212
"struct_class",
1313
slots=c(name='character',
1414
description="character",
15-
type="character"
15+
type="character",
16+
libraries='character'
1617
)
1718
)
1819

20+
setMethod('initialize','struct_class',function(.Object,...) {
21+
22+
L=list(...)
23+
SN=slotNames(.Object)
24+
if (length(L)>0)
25+
{
26+
for (i in seq_len(length(L)))
27+
{
28+
if (names(L)[[i]] %in% SN) {
29+
slot(.Object,names(L)[[i]])=L[[names(L)[[i]]]]
30+
} else if (is(.Object,'parameter_class')) {
31+
param.value(.Object,names(L)[[i]])=L[[names(L)[[i]]]]
32+
} else {
33+
stop(paste0(names(L)[[i]], 'is not a valid for ', class(.Object), ' objects.'))
34+
}
35+
36+
}
37+
}
38+
39+
# check if packages are available
40+
not_found=character(0)
41+
for (k in .Object@libraries) {
42+
if (!requireNamespace(k, quietly = TRUE)) {
43+
not_found=c(not_found,k)
44+
}
45+
}
46+
47+
if (length(not_found)>0) {
48+
stop(paste0('The following packages are required but not installed: ', paste0(not_found,collapse=', ',
49+
'Please install them.')),
50+
call. = FALSE)
51+
}
52+
53+
return(.Object)
54+
55+
})
56+
57+
1958
#' name
2059
#'
2160
#' get the name of an object

0 commit comments

Comments
 (0)