![]() Memoize_first <- function ( fun ) say_hello ( name ) say_goodbye ( name ) Say we want to cache a function that only sleeps. The memoise package by Jim Hester is easy to use. Here’s a roundup of some ways to cache results of functions in R. (See internal miniconda_install_prompt()). As an example, reticulate will ask you once if you want to install miniconda by storing your answer locally if you say no and not ask again. It could be per session caching but also persistent caching. The polite package for polite webscraping caches results.Ĭaching can be about results of functions but also some user inputs that won’t change for the session and you don’t want to ask every time you need it (being polite!). That’s similar to the second item but from the perspective of e.g. a web API you keep hitting when you could have saved the result. It might help save other resources of users such as money: e.g. if the function calls a web API whose pricing depends on the number of hits. You will often think of caching as something valid in only one R session, but we’ll see it can be persistent across sessions via storage on disk. Every other time the function is called with the same input, the result is retrieved from the cache unless invalidated. The result is stored in a cache of some sort (more practical details later!). ![]() Caching: what is it and why use it?Ĭaching means that if you call a function several times with the exact same input, the function is only actually run the first time. How about for a function with the same inputs (or with no argument!), we only run it once e.g. per R session, and save the results for later? In this post, we shall go over ways to cache results of R functions, so that you don’t need to burden machines and humans. You could also interpret it as don’t let the machine repeat its calculations if useless. One principle of programming that’s often encountered is “DRY”, “Don’t Repeat Yourself”, that encourages e.g. the use of functions over duplicated (read: copy-pasted and slightly amended) code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |