Other sites

‘mustashe’ Explained

[This article was first published on Posts | Joshua Cook, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The purpose of the ‘mustashe’ R package is to save objects that result
from some computation, then load the object from file the next time the
computation is performed. In other words, the first time a chunk of code
is evaluated, the output can be stashed for the next time the code
chunk is run.

How ‘mustashe’ works

Overview

The following is the actual code for the stash() function, the main
function of the ‘mustashe’ package. I have only added a few more
comments for clarification.

stash

Overall, I believe the logic is quite simple. The steps that thestash() function follows, further explained in the following sections,
are:

Deparse and format the code.

Make a hash table based on the code and dependencies.

If the object has previously been stashed, then the new hash table
and the stashed one are compared. If they are the same, then the
object is loaded from memory.

If the hash tables are different or the object has never been
stored, then the code is evaluated and the object and its hash table
are stashed.

Deparsing and formatting code

The first step taken by the stash() function is to deparse and format
the code.

Deparsing the code means to turn the unevaluated expression into a
string. The deparsing is done by passing the code immediately tosubstitute() and deparse(). This must be done immediately, else the
code will be evaluated. The substitute() function “returns the parse
tree for the (unevaluated) expression expr, substituting any variables
bound in env.”

substitute(x

#> x

The deparse() function “Turn[s] unevaluated expressions into
character strings.” Paired with substitute(), it returns a string of
the unevaluated code.

deparse(substitute(x

#> [1] "x

With the code now as a string, it is formatted using the tidy_source()
function from‘formatR’.
An internal function in ‘mustashe’, format_code() handles this
process:

format_code

#> [1] "x

The purpose of formatting the code is so any stylistic changes to thecode input do not affect the hash table. To demonstrate this, notice
how the output from format_code() is the same between the two
different code examples.

format_code("x=2")

#> [1] "x

format_code(("x

#> [1] "x

Making a hash table

The hash table is a two-column table with the name and hash value of the
code and any (optional) dependencies.

The hashing is handled by the‘digest’
package. It takes a value and reproducibly produces a unique hash value.

digest::digest("mustashe")

#> [1] "ac2aad9fdb730500c56009bff6154a7e"

A hash value is made for the code and for any of the dependencies linked
to the object. This process is handled by the make_hash_table(code,
depends_on) internal function.

Comparing hash tables

To tell if the code or dependencies have changed, the new hash table and
stashed hashed table are compared. The function underlying this process
is all.equal() from base R. This function compares two objects and “If
they are different, [a] comparison is still made to some extent, and a
report of the differences is returned.”

Here is an example of using all.equal() to compare two data frames.

# Two data frames with a small difference *
df1

#> [1] TRUE

# When the two data frmaes are not equivalent.
all.equal(df1, df2)

#> [1] "Component \"b\": Mean relative difference: 0.1428571"

A word of caution, if using all.equal() for a boolean comparison (like
in an if-statement), make sure to wrap it with isTRUE, otherwise it
will return TRUE or comments on the differences, but not FALSE.

Evaluating code and making a stash

If the hash tables are different, that means the code must be evaluated,
the new object be assigned to the desired name (var), and the new hash
table and value stashed. This is handled by the internal functionnew_stash().

# Make a new stash from a variable, code, and hash table.
new_stash

The first step is to evaluate the code with the evaluate_code(code)
function. It uses the parse() and eval() functions and returns the
resulting value.

# Evaluate the code in a new environment.
evaluate_code

This value is then assigned the desired name in the global environment
using the internal assign_value(var, val) function, where .TargetEnv
is a variable in the package pointing to .GlobalEnv.

# Assign the value `val` to the variable `var`.
assign_value

Lastly, the hash table and value are written to file using wrapper
functions around readr::write_tsv() and saveRDS().

Contact

Any issues and feedback on ‘mustashe’ can be submittedhere. I can be reached
through the contact form on my website
or on Twitter @JoshDoesa.