knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
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
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 })
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.