Just an example of how to use the
defmacro
package.
The package exports a function called analyze_dataset
with the
following code that defines its own pipe function and a zero overhead
logging macro.
analyze_dataset <- function(data) {
log("Computing result")
assert("is a data frame", stopifnot(is.data.frame(data)))
assert("has right columns", stopifnot(c("hp", "cyl") %in% colnames(data)))
result <- data %>%
dplyr::filter(hp > constexpr(50 + 50 + qnorm(0.975))) %>%
dplyr::group_by(cyl) %>%
dplyr::summarise(dplyr::n())
log("Returning result")
result
}
During package load, the code gets replaced by the expanded version of the macro. This means the function does not have any runtime overhead.
defmacroex::analyze_dataset
#> function (data)
#> {
#> result <- dplyr::summarise(dplyr::group_by(dplyr::filter(data,
#> hp > 101.95996398454), cyl), dplyr::n())
#> result
#> }
#> <bytecode: 0x7fc16f83fe70>
#> <environment: namespace:defmacroex>
Had we enabled logging through an environment variable, the logging code would have been added to the function:
unloadNamespace("defmacroex")
withr::with_envvar(c("LOGGING" = "1", "ASSERT" = "1"), {
defmacroex::analyze_dataset
})
#> function (data)
#> {
#> message("Computing result")
#> stopifnot(is.data.frame(data))
#> stopifnot(c("hp", "cyl") %in% colnames(data))
#> result <- dplyr::summarise(dplyr::group_by(dplyr::filter(data,
#> hp > 101.95996398454), cyl), dplyr::n())
#> message("Returning result")
#> result
#> }
#> <bytecode: 0x7fc16eaa42f8>
#> <environment: namespace:defmacroex>
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.