setAltSelfData: Set the data for an altWrapper object in ALTREP functions

Description Usage Arguments Value Examples

View source: R/altWrapper-tools.R

Description

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.

Usage

1

Arguments

x

The data of an altWrapper object

Value

no return value

Examples

 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)

Jiefei-Wang/AltWrapper documentation built on Oct. 30, 2019, 7:43 p.m.