Source a R script, only if the result has not already been stashed. The final expression in the script is treated as the return value from the script (as per the functionality of source(), and this value is returned and stashed (using stash() as the underlying caching mechanism).

stash_script(script_path, depends_on = NULL, verbose = NULL)

Arguments

script_path

Path (absolute or relative) to the script to source.

depends_on

A vector of other objects that this one depends on. Changes to these objects will cause the re-running of the code next time.

verbose

Whether to print action statements (default TRUE).

Value

The last line in the script referred to by the script at script_path is treated as a return value, and is returned and cached.

Details

This function always calls stash() in functional mode.

Note that the stashed result is keyed according to the specified path to the script without any canonization. The function will therefore not realize that an absolute and relative path refer to the same file, and treat them as two separate results to cache.

The time of modification is also included in the key of the results so any modification to the file will result in the script being re-run on the next call.

Examples

script_name <- tempfile()
set.seed(42)
write("sample(letters,5)", script_name)
x <- stash_script(script_name) # will be cached
#> Stashing object.
print(x)
#> [1] "q" "e" "a" "j" "d"
x <- stash_script(script_name) # will use cached data
#> Loading stashed object.
print(x)
#> [1] "q" "e" "a" "j" "d"