The goal of deflist is to provide a list-like object for which element access is routed through a user-supplied function. This can be used, for example, when list-like syntax is required but one cannot afford to hold all elements in memory at once.
Installation
You can install the released version of deflist from CRAN with:
install.packages("deflist")And the development version from GitHub with:
# install.packages("remotes")
remotes::install_github("bbuchsbaum/deflist")Examples
library(deflist)
dl <- deflist(function(i) i, len=10)
print(dl[[1]])
#> [1] 1
calls <- integer()
dl2 <- deflist(function(i) {
calls <<- c(calls, i)
i * 2
}, len = 10)
print(dl2[[1]])
#> [1] 2
print(dl2[[3]])
#> [1] 6
print(calls)
#> [1] 1 3The value at an index may change across calls, for example:
set.seed(1)
dl3 <- deflist(function(i) { rnorm(1) }, len=10)
print(dl3[[1]])
#> [1] -0.6264538
print(dl3[[1]])
#> [1] 0.1836433Memoisation can be enabled so that values at a given index are cached:
set.seed(1)
dl4 <- deflist(function(i) { rnorm(1) }, len=10, memoise=TRUE)
print(dl4[[1]])
#> [1] -0.6264538
print(dl4[[1]])
#> [1] -0.6264538In addition, memoisation can be set to store cached values to the file system:
set.seed(1)
dl5 <- deflist(function(i) { rnorm(1000) }, len=10, memoise=TRUE, cache="file", cachedir = tempdir())
print(dl5[[1]][1:10])
#> [1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684
#> [7] 0.4874291 0.7383247 0.5757814 -0.3053884
print(dl5[[1]][1:10])
#> [1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684
#> [7] 0.4874291 0.7383247 0.5757814 -0.3053884