<- function(x, y) { x + y }
f attributes(f)
$srcref
function(x, y) { x + y }
formals(f)
$x
$y
Steven P. Sanderson II, MPH
February 1, 2023
R is a powerful programming language that is widely used for data analysis, visualization, and machine learning. One of the features of R that makes it versatile and flexible is the ability to assign attributes to functions. Attributes are metadata associated with an object in R, and they can be used to store additional information about the function or to modify the behavior of the function.
In this blog post, we will discuss what attributes are, how they can be useful, and how they can be used inside R functions.
Attributes are pieces of information that are stored alongside an object in R. Functions are objects in R, and they can have attributes associated with them. Some of the common attributes associated with functions in R include:
formals
: This attribute stores the arguments of the function and their default values.srcref
: This attribute stores the source code of the function, including the line numbers of the code.environment
: This attribute stores the environment in which the function was defined.Attributes can be useful in R functions in several ways, including:
To access or modify the attributes of a function in R, you can use the attributes() function. For example, to retrieve the formals attribute of a function, you can use the following code:
To add an attribute to a function, you can use the attr() function. For example, to add a version attribute to a function, you can use the following code:
$srcref
function(x, y) { x + y }
$version
[1] "1.0"
To remove an attribute from a function, you can use the attributes() function with the NULL value. For example, to remove the version attribute from a function, you can use the following code:
f <- function(x, y) { x + y }
attr(f, "version") <- "1.0"
attributes(f)$version <- NULL
attributes(f)
$srcref
function(x, y) { x + y }
Conclusion
Attributes are a useful feature in R functions that can be used to store additional information about the function, to debug the function, and to modify its behavior. By using attributes, you can make your functions more versatile, flexible, and easier to work with.
Here is a function from my {healthyR.ts}
package that makes use of attributes that come in from the output of another function. The function is ts_brownian_motion_plot()
. Let’s go ahead and take a look at how the function works.
# ...
# Attributes
atb <- attributes(.data)
# ...
ggplot2::labs(
title = atb$.motion_type,
subtitle = paste0("Simulations: ", atb$.num_sims,
" - Initial Value: ", round(atb$.initial_value, 2),
" - Delta Time: ", round(atb$.delta_time, 2))
) +
ggplot2::theme(legend.position = if(atb$.num_sims > 9) {"none"})
So what’s happening here is that I am taking the attributes from the incoming data which is the result of a {healthyR.ts}
brownian motion function and setting them equal to a variable atb
, later in the function I take that atb
variable and pluck out certain items I want from it.
Let’s see an example in action.
# install.packages("healthyR.ts")
library(healthyR.ts)
df <- ts_brownian_motion()
atb <- attributes(df)
atb[!names(atb) %in% "row.names"]
$names
[1] "sim_number" "t" "y"
$class
[1] "tbl_df" "tbl" "data.frame"
$.time
[1] 100
$.num_sims
[1] 10
$.delta_time
[1] 1
$.initial_value
[1] 0
$.return_tibble
[1] TRUE
$.motion_type
[1] "Brownian Motion"
Now to see the plat in action.
Voila!