Description Usage Arguments Value Examples
View source: R/altWrapper-tools.R
Set the data for an altWrapper object inside an ALTREP function,
The function is only allowed to be called in an ALTREP API.
It is designed for caching data for an altWrapper object(eg. sum value,
a block of on-disk data). It CANNOT be used to change the value of the
altWrapper object.
Please refer to ?setAltMethod
to find a full list of the ALTREP APIs.
1 |
x |
The data of an altWrapper object |
no return value
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 | ## The data is a list
data <- list(data=runif(10))
## Define the ALTREP functions
length_func <- function(x) length(x$data)
get_ptr_func <- function(x,writeable) x$data
## Define a sum function,
## after the fisrt computation,
## store the result in the list.
## Call `setAltSelfData` to set the data
sum_func <- function(x, na.rm) {
if(is.null(x$sum)){
message("computing sum")
x$sum <- sum(x$data)
setAltSelfData(x)
return(x$sum)
}else{
message("reading sum result")
return(x$sum)
}
}
## Define the altWrapper class and its functions
setAltClass(className = "example", classType = "double")
setAltMethod(className = "example", getLength = length_func)
setAltMethod(className = "example", getDataptr = get_ptr_func)
setAltMethod(className = "example", sum = sum_func)
A <- newAltrep(className = "example", x = data)
#First sum call
sum(A)
#Second sum call
sum(A)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.