11.6 Cache large objects
When the chunk option cache = TRUE
, cached objects will be lazy-loaded into the R session, which means an object will not be read from the cache database until it is actually used in the code. This can save you some memory when not all objects are used later in the document. For example, if you read a large data object but only use a subset in the subsequent analysis, the original data object will not be loaded from the cache database:
```{r, read-data, cache=TRUE}
full <- read.csv("HUGE.csv")
rows <- subset(full, price > 100)
# next we only use `rows`
```
```{r}
plot(rows)
```
However, when an object is too large, you may run into an error like this:
in lazyLoadDBinsertVariable(vars[i], ...
Error : ...
long vectors not supported yet Execution halted
If this problem occurs, you can try to turn off the lazy-loading via the chunk option cache.lazy = FALSE
. All objects in this chunk will be immediately loaded into memory.