forked from RConsortium/S7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.R
45 lines (41 loc) · 1.04 KB
/
data.R
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
#' Get/set underlying "base" data
#'
#' When an S7 class inherits from an existing base type, it can be useful
#' to work with the underlying object, i.e. the S7 object stripped of class
#' and properties.
#'
#' @inheritParams prop
#' @param value Object used to replace the underlying data.
#' @return `S7_data()` returns the data stored in the base object;
#' `S7_data<-()` is called for its side-effects and returns `object`
#' invisibly.
#' @export
#' @examples
#' Text <- new_class("Text", parent = class_character)
#' y <- Text(c(foo = "bar"))
#' y
#' S7_data(y)
#'
#' S7_data(y) <- c("a", "b")
#' y
S7_data <- function(object) {
check_is_S7(object)
zap_attr(object, c(prop_names(object), "class", "S7_class"))
}
#' @export
#' @rdname S7_data
`S7_data<-` <- function(object, check = TRUE, value) {
attrs <- attributes(object)
object <- value
attributes(object) <- attrs
if (isTRUE(check)) {
validate(object)
}
return(invisible(object))
}
zap_attr <- function(x, names) {
for (name in names) {
attr(x, name) <- NULL
}
x
}