limit_rate {ratelimitr}R Documentation

Limit the rate at which a function will execute

Description

Limit the rate at which a function will execute

Usage

limit_rate(f, ..., precision = 60)

## S3 method for class 'list'
limit_rate(f, ..., precision = 60)

## S3 method for class 'function_list'
limit_rate(f, ..., precision = 60)

## S3 method for class 'function'
limit_rate(f, ..., precision = 60)

Arguments

f

A single function to be rate-limited, or a named list of functions

...

One or more rates, created using rate

precision

The precision with which time intervals can be measured, in hertz

Value

If f is a single function, then a new function with the same signature and (eventual) behavior as the original function, but rate limited. If f is a named list of functions, then a new list of functions with the same names and signatures, but collectively bound by a shared rate limit.

See Also

rate, UPDATE_RATE

Examples

## limiting a single function
f <- limit_rate(Sys.time, rate(n = 5, period = .1))
res <- replicate(10, f())
## show the elapsed time between each function call:
round(res[-1] - head(res, -1), 3)

## for multiple functions, make sure the list is named:
f <- function() 1
g <- function() 2
limited <- limit_rate(list(f = f, g = g), rate(n = 1, period = .1))
system.time({limited$f(); limited$g()})


[Package ratelimitr version 0.4.1 Index]