# A a grep function with x as first param is often useful
zgrep <- zfunction(grep, x)
carnames <- rownames(mtcars)
grep("ll", carnames, value=TRUE)
zgrep(carnames, "ll", value=TRUE)
# zfunction() is the best approach to wrapping functions such as
# `pls::plsr()` that hide the data parameter behind the `...`.
if (requireNamespace("pls")) {
zplsr <- zfunction(pls::plsr, data, x_not_found = "ok")
zplsr(cars, dist ~ speed)
}
# Curly {x} handling: These are all equivalent
param_name <- "to";
f1 <- zfunction(file.rename, to)
f2 <- zfunction(file.rename, "to")
f3 <- zfunction(file.rename, {param_name})
# Using zfold() to create a grep() wrapper with the desired arg order
zgrep <- zfold(grep, x)
carnames <- rownames(mtcars)
grep("ll", carnames, value=TRUE)
zgrep(carnames, "ll", value=TRUE)
# Using zfitter to wrap around a fitting function
# (this is the actual way zlm_robust is defined in this package)
if (requireNamespace("estimatr", quietly = TRUE)) {
zlm_robust <- zfitter(estimatr::lm_robust)
zlm_robust(cars, speed~dist)
# The resulting function works well the native pipe ...
if ( getRversion() >= "4.1.0" ) {
cars |> zlm_robust( speed ~ dist )
}
}
# ... or with dplyr
if ( require("dplyr", warn.conflicts=FALSE) ) {
# Pipe cars dataset into zlm_robust for fitting
cars %>% zlm_robust( speed ~ dist )
# Process iris with filter() before piping. Print a summary()
# of the fitted model using zprint() before assigning the
# model itself (not the summary) to m.
m <- iris %>%
dplyr::filter(Species=="setosa") %>%
zlm_robust(Sepal.Length ~ Sepal.Width + Petal.Width) %>%
zprint(summary)
}
Run the code above in your browser using DataLab