knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(pryr)
trimmer
0.7.5 is now available on CRAN.
trimmer
is a lightweight toolkit to trim a (potentially big) R object without
breaking the results of a given function call, where the (trimmed) R object
is given as argument.
The trim
function is the bread and butter of trimmer
. It seeks to reduce
the size of an R object by recursively removing elements from the object
one-by-one. It does so in a 'greedy' fashion - it constantly tries to
remove the element that uses the most memory.
The trimming process is constrained by a reference function call. The trimming procedure will not allow elements to be removed from the object, that will cause results from the function call to diverge from the original results of the function call.
There can be many data reasons as to why, you might want to 'trim' an R object.
A typical example could be a R model object. It will typically contain all kinds of (more or less useful) stuff and meta data with information about the model. You might want to try to reduce the size of the object for (memory) efficiency purposes, such that the model only contains only what is in fact needed to predict new observations - and nothing else!
Install the development version of trimmer
with:
remotes::install_github("smaakage85/trimmer")
Or install the version released on CRAN:
install.packages("trimmer")
The trimming procedure - conducted with trim()
- consists of the following steps:
Get ready by loading the package.
library(trimmer)
Train a model on the famous mtcars
data set.
# load training data. trn <- datasets::mtcars # estimate model. mdl <- lm(mpg ~ ., data = trn)
I want to trim the model object mdl
as possible without affecting the predictions,
computed with function predict()
, for the resulting model.
The trimming is then simply conducted by invoking:
mdl_trim <- trim(obj = mdl, obj_arg_name = "object", fun = predict, newdata = trn)
And that's it!
Note, that I provide the trim
function with the extra argument newdata
, that
is passed to the function call with fun
. This means, that the trimming is
constrained by, that the results of 'fun' (=predict
) MUST be exactly the same
on these data before and after the trimming.
The trimmed model object now measures r pf_obj_size(object_size(mdl_trim))
. The original
object measured r pf_obj_size(object_size(mdl))
.
If you just want the object size to be below some threshold, you can set that as a criterion. The 'trimming' process will continue no further, when this threshold is reached. This approach can be time-saving compared to minimizing the object as much as possible (=default setting).
mdl_trim <- trim(obj = mdl, obj_arg_name = "object", fun = predict, newdata = trn, size_target = 0.015)
With these settings, the trimmed model object measures r pf_obj_size(object_size(mdl_trim))
. The original
object measured r pf_obj_size(object_size(mdl))
.
trimmer
is compatible with all R objects, that inherit from the list
class - not just R model objects - and all kinds of functions - not just the
predict function
. Hence trimmer
is quite a flexible tool.
To illustrate I will trim the same object but under the constraint, that the
results from the summary()
function must be preserved.
mdl_trim <- trim(obj = mdl, obj_arg_name = "object", fun = summary)
You can choose whether or not to tolerate warnings from reference function calls with argument tolerate_warnings
.
You can also choose, that certain elements MUST NOT be removed in the trimming process. Do this with the dont_touch
argument.
I would like to extend the framework to also support parallellization.
That is it, I hope, that you will enjoy the trimmer
package :)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.