Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

caching

Remember the past to be quicker in the future


Description

Caching makes styler faster on repeated styling and is shared across all APIs (e.g. style_text() and Addin). That means if you style code that already complies to a style guide and you have previously styled that code, it will be quicker.

Manage the cache

See cache_info(),cache_activate() or cache_clear() for utilities to manage the cache. You can deactivate it altogether with cache_deactivate(). Since we leverage {R.cache} to manage the cache, you can also use any {R.cache} functionality to manipulate it.

Interactive setup

{styler} by default uses caching via the {R.cache} package. When interacting with {styler}, you will be asked to let it create a permanent cache on your file system that styler will use in case it is not set already up for another tool that uses {R.cache}. We encourage users to let {R.cache} create a permanent directory for caching because, otherwise, the cache is lost at restart of R.

Non-interactive setup

In some cases, you want to avoid the interactive prompt described above. In that situation, you can set the path to the cache with the R option R.cache.rootPath or the environment variable R_CACHE_ROOTPATH to an existent path before you call the styler API. This should avoid the prompt. R.cache::setCacheRootPath("/path/to/cache") is also programmatic but will probably give the prompt anyways if called interactively.

Non-interactive use

Note that if you have never authorized {R.cache} to create the cache in a permanent directory and you use {styler} non-interactively, it will build the cache in a temporary directory. To create a permanent cache, follow the section 'Non-interactive setup' or 'Interactive setup' above.

Invalidation

The cache is specific to a version of styler by default, because different versions potentially format code differently. This means after upgrading styler or a style guide you use, the cache will be re-built.

Mechanism and size

The cache works by storing hashed output code as a whole and by expression, which is why it takes zero space on disk (the cache is a directory with empty files which have the hash of output code as name).

Using a cache for styler in CI/CD

If you want to set up caching in a CI/CD pipeline, we suggest to set the {R.cache} root path to a directory for which you have the cache enabled as described above in the section 'Non-interactive setup'. This can often be set in config files of CI/CD tools, e.g. see the Travis documentation on caching.

See Also

Other cache managers: cache_activate(), cache_clear(), cache_info()


styler

Non-Invasive Pretty Printing of R Code

v1.4.1
MIT + file LICENSE
Authors
Kirill Müller [aut], Lorenz Walthert [cre, aut]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.