List the arguments to a function

args(function_name)

Categorical defaults

function(x, y, z, cat_arg = c("arg1", "arg2", "arg3"))
# inside function body
cat_arg <- match.arg(cat_arg)

Passing arguments between functions

function(x, ...) {
  some_other_function(...)
}

In cases where some_other_function in our case takes many arguments that we want to specify as the user sends them, we can use ellipsis like this to get around with it.

Checking for values type manually

if(! is.some_type(x)) {
  stop("The variable x doesn't comply to class of some_type. Instead it has ", class(x), ".")
}

Asserting Checks

assert_is_some_type(x)

Change the type of something

coerce_to(use_first(any_var), target_class = "class")

Often in using TRUE or FALSE this works when people just type something rather than full thing. In such ways the user input errors can be handled.

Zeallot package Multi Assignment

library(zeallot)
c(arg_1, arg_2, arg_3) %<-% multi_output_function()

This gives the output as multiple values that can later be used.

Attributes

attributes(some_var) # gets the attributes list
attr(some_var, "names") # gets the name attribute for the variable
attr(some_var, "names") <- name_attr # sets the name attribute

Broom Package

glance()
tidy()

List of attributes

ls.str(list_obj)
env_list <- list2env(list_obj)
ls.str(env_obj)

The both returns similar objects.

Environments

  • Grandparent environment

    grandparent <- parent.env(parent)
    environmentName(grandparent)
    
  • Search loaded environments

    search()
    
  • Exists Function

    exists("variable", envir = env_obj)
    
  • Accessing variable outside functions

Using operators with pipe

x %>% raise_to_power(some_num)
x %>% multiply_by(some_num)

Returning Invisibly

invisible(ret_val)