grrr
: A package for modifying default arguments Things that make you go “grrr!”.
R
has some default arguments that can make programming frustrating and
error-prone - stringsAsFactors = TRUE
immediately springs to mind.
This package offers a way to modify default arguments to functions and is a tool for exploring:
The package offers three main functions:
default_args_to_global_env()
- read the default arguments from a
function and place their values in the global environment. This is
useful for debugging.update_function_arguments()
- overwrite default arguments in
functions (even within packages!)set_sentinel_on_default_arg()
- adjust a function to simplify
notify you when it’s using a default argument.Use grrr
at your own risk!
Changing default arguments for functions is fraught with danger and will causes problems if you aren’t extremely careful.
# install.packages("devtools")
devtools::install_github("coolbutuseless/grrr")
default_args_to_global_env()
copies all of the default arguments for a
function into the global environment.
I find this handy when I’m debugging a function with a lot of default arguments that I don’t want to manually copy/define in order to run bits of code line-by-line in a function.
ls()
#> character(0)
myfun <- function(a = 1, b = 2, c = 3, d = 4, e = 5) {
sum(c(a, b, c, d, e))
}
grrr::default_args_to_global_env(function_name = 'myfun')
#> Assigning: a <- 1
#> Assigning: b <- 2
#> Assigning: c <- 3
#> Assigning: d <- 4
#> Assigning: e <- 5
ls()
#> [1] "a" "b" "c" "d" "e" "myfun"
a
#> [1] 1
update_function_arguments()
will change the default values for an
argument to any function in the current environment, or within a
package.
E.g. in the following code, we change the builtin rnorm()
function to
have mean = 100
rather than the default of mean = 0
set.seed(1); stats::rnorm(5)
#> [1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078
# Change the default value for `mean` from `0` to `100`
grrr::update_function_arguments(function_name = 'rnorm', package_name = 'stats', mean = 100)
set.seed(1); stats::rnorm(5)
#> [1] 99.37355 100.18364 99.16437 101.59528 100.32951
We can also remove the default for mean
altogether so that it has to
be specified every time the function is called
# Remove the default value for `mean`
grrr::update_function_arguments(function_name = 'rnorm', package_name = 'stats', mean =)
set.seed(1); stats::rnorm(5)
Error in stats::rnorm(5) :
argument "mean" is missing, with no default
You are strongly advised against doing this.
The best(?) usage for grrr
will come from using it for debugging in
such a way that the default arguments are still used, but you can
discover where they are being used.
set_sentinel_on_default_arg()
is a wrapper around
update_function_arguments()
that will rewrite the default argument so
that it prints a message and call-stack whenever the function is called
and the default argument has not been specified by the user.
E.g. to make data.frame()
noisy so that the user is notified whenever
an attempt was made to use the function without explicitly setting
stringsAsFactors
. The function will still use the default value - it
will just be noisier when it
does.
grrr::set_sentinel_on_default_arg('data.frame', 'stringsAsFactors', package_name = 'base')
data.frame(x = 1:3, y = c('a', 'b', 'c'))
Using default argument for 'stringsAsFactors' in function 'data.frame':
>> data.frame(x = 1:3, y = c("a", "b", "c"))
x y
1 1 a
2 2 b
3 3 c
This package uses unlockBinding()
and assignInNamespace()
and can
mess with the internals of other packages. This is generally frowned
upon.
Furthermore, assignInNamespace()
actually has some checks to prevent
what this package tries to do! This is why grrr
includes
sudo_assignInNamespace()
which drops some sanity checks and and allows
us to stomp all over other packages and namespaces.
Because of these devilish things the package has to do, grrr
will
never appear on CRAN.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.