Skip to content

Instantly share code, notes, and snippets.

@ivopbernardo
Last active November 2, 2022 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivopbernardo/e66bdd48f6877c4ee1bfdaf9d5cf70d0 to your computer and use it in GitHub Desktop.
Save ivopbernardo/e66bdd48f6877c4ee1bfdaf9d5cf70d0 to your computer and use it in GitHub Desktop.
Blog Post - Function Best Practices
# R Function Best Practices used in blog post:
# https://towardsdatascience.com/writing-better-r-functions-best-practices-and-tips-d48ef0691c24
library(ggplot2)
#----------------------------------#
# Function Indentation
# Proper Indentation - Bad Example
print_names <- function(vector_names) {
for (name in vector_names) {
print(paste('Hello',name))
}
}
# Proper Indentation - Good Example
print_names <- function(vector_names) {
for (name in vector_names) {
print(paste('Hello',name))
}
}
print_names(c('Joe','Mary','Anne'))
# Get Car Brand - Not Indented
get_car_brand <- function(car_model) {
car_brand <- sapply(strsplit(car_model, ' '), '[', 1)
return (car_brand)
}
# Get Car Brand - Indented
get_car_brand <- function(car_model) {
car_brand <- sapply(
strsplit(car_model, ' '),
'[',
1
)
return (car_brand)
}
get_car_brand(rownames(mtcars))
# Shorter Functions over Long Functions
plot_average_horsepower_brand <- function(cars_df) {
cars_df_copy <- cars_df
cars_df_copy['car_brand'] <- sapply(
strsplit(rownames(cars_df_copy), ' '),
'[',
1
)
aggregate_brand <- aggregate(
cars_df_copy$hp,
by = list(cars_df_copy$car_brand),
FUN = mean
)
sort_order <- factor(
aggregate_brand[order(aggregate_brand[,'x']),]$Group.1
)
ggplot(
data = aggregate_brand,
aes(x=factor(Group.1, levels=sort_order), y=x, color='darkred')
) + geom_point() + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
}
plot_average_horsepower_brand(mtcars)
# Breaking down our function into multiple blocks
create_brand <- function(cars_df) {
brands <- sapply(
strsplit(rownames(cars_df), ' '),
'[',
1
)
return (brands)
}
mean_by_variable <- function(df, agg_var, by_var) {
aggregate_brand <- aggregate(
df[,agg_var],
by = list(df[,by_var]),
FUN = mean
)
return (aggregate_brand)
}
plot_sorted_scatter <- function(cars_data, agg_var, by_var) {
# Add Brand
cars_data$brand <- create_brand(cars_data)
# Create Aggregation
agg_data <- mean_by_variable(cars_data, agg_var, by_var)
# Sort
sort_order <- factor(
agg_data[order(agg_data[,'x']),]$Group.1
)
ggplot(
data = agg_data,
aes(x=factor(Group.1, levels=sort_order), y=x, color='darkred')
) + geom_point() + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
}
plot_sorted_scatter(mtcars, 'hp', 'brand')
# Doc Strings
return_inverse <- function(x) {
#' Computes the multiplicative inverse of the input
return(1/x)
}
?return_inverse
library(docstring)
?return_inverse
# Return Inverse with Title
return_inverse <- function(x) {
#' Multiplicative Inverse of Number
#'
#' @description Computes the multiplicative inverse of the input
#'
#' @param x: Real number.
return(1/x)
}
?return_inverse
# Explicit vs. Implicit Returns
create_brand_explicit <- function(cars_df) {
brands <- sapply(
strsplit(rownames(cars_df), ' '),
'[',
1
)
return (brands)
}
create_brand_implicit <- function(cars_df) {
sapply(
strsplit(rownames(cars_df), ' '),
'[',
1
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment