Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions R/quick.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#' Compile an R function.
#'
#' @param fun An R function
#' @param name Optional string, name to use for the function.
#' @param name String, name to use for the function. This is optional in
#' regular usage but required in an R package. As a convenience, arguments
#' `fun` and `name` can also be supplied as positional arguments to `quick` with
#' `name` in the first position.
#'
#' @details
#'
Expand All @@ -12,8 +15,9 @@
#' The shape and mode of all function arguments must be declared. Local and
#' return variables may optionally also be declared.
#'
#' `declare(type())` also has support for declaring size constraints, or size
#' relationships between variables. Here are some examples of declare calls:
#' `declare(type())` also has support for declaring size constraints, or
#' size relationships between variables. Here are some examples of declare
#' calls:
#'
#' ```r
#' declare(type(x = double(NA))) # x is a 1-d double vector of any length
Expand Down Expand Up @@ -64,11 +68,11 @@
#'
#' ## Return values
#'
#' The shape and type of a function return value must be known at compile time.
#' In most situations, this will be automatically inferred by `quick()`. However,
#' if the output is dynamic, then you may need to provide a hint.
#' For example, returning the result of `seq()` will fail because the output shape
#' cannot be inferred.
#' The shape and type of a function return value must be known at compile
#' time. In most situations, this will be automatically inferred by
#' `quick()`. However, if the output is dynamic, then you may need to
#' provide a hint. For example, returning the result of `seq()` will fail
#' because the output shape cannot be inferred.
#'
#' ```r
#' # Will fail to compile:
Expand All @@ -82,8 +86,8 @@
#' })
#' ```
#'
#' However, if the output size can be declared as a dynamic expression using other
#' values known at runtime, compilation will succeed:
#' However, if the output size can be declared as a dynamic expression using
#' other values known at runtime, compilation will succeed:
#'
#' ```r
#' # Succeeds:
Expand Down Expand Up @@ -145,6 +149,12 @@ quick <- function(fun, name = NULL) {

pkgname <- parent.pkg()
if (!is.null(pkgname) && pkgname != "quickr") {
if (startsWith(name, 'anonymous_quick_function_')) {
stop(
'When used in an R package, you must provide a unique `name` to every `quick()` call.\n',
'For example: `my_fun <- quick("my_fun", function(x) ....)'
)
}
# we are in a package - but outside a quickr::compile_package() call.
return(create_quick_closure(name, fun))
}
Expand Down
6 changes: 6 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,12 @@ plot(timings) + bench::scale_x_bench_time(base = NULL)
When called in a package, `quick()` will pre-compile the quick functions and place them in the `./src` directory.
Run `devtools::load_all()` or `quickr::compile_package()` to ensure that the generated files in `./src` and `./R` are in sync with each other.

In a package, you must provide a function name to `quick()`. For example:

```r
my_fun <- quick(name = "my_fun", function(x) ....)
```

## Installation

You can install quickr from CRAN with:
Expand Down
24 changes: 14 additions & 10 deletions man/quick.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading