Skip to content

Instantly share code, notes, and snippets.

@cannin
Created December 18, 2024 13:01
Show Gist options
  • Save cannin/7eef93e121e9a3534c8d50866a9ad413 to your computer and use it in GitHub Desktop.
Save cannin/7eef93e121e9a3534c8d50866a9ad413 to your computer and use it in GitHub Desktop.
Summarize R Environment Variables
library(ggplot2)
library(yaml)
# Function to summarize a ggplot object
# TODO: dendrograms with hclust do not work
summarize_ggplot <- function(object) {
wrap <- function(x) paste(
paste(strwrap(x, exdent = 2), collapse = "\n"),
"\n", sep = ""
)
summary_list <- list()
if (!is.null(object$data)) {
summary_list$data <- list(
columns = names(object$data),
dimensions = list(rows = nrow(object$data), columns = ncol(object$data))
)
}
if (length(object$layers) > 0) {
summary_list$layers <- paste(sapply(object$layers, function(layer) class(layer$geom)[1]), collapse = ", ")
}
return(summary_list)
}
# Function to summarize environment and format as YAML
summarize_environment_yaml <- function(func_char_limit = 50) {
# Get the names of all objects in the global environment
obj_names <- ls(envir = .GlobalEnv)
# Initialize a list to store summaries
obj_summaries <- list()
# Loop through each object and summarize
for (obj_name in obj_names) {
obj <- get(obj_name, envir = .GlobalEnv) # Retrieve the object
obj_type <- class(obj)[1] # Get the object's primary class
obj_size <- format(object.size(obj), units = "auto") # Get the object's size
# Generate the appropriate summary structure
obj_summary <- tryCatch(
{
if (is.data.frame(obj) || is.matrix(obj)) {
list(
dimensions = list(
rows = nrow(obj),
columns = ncol(obj)
)
)
} else if (is.character(obj)) {
list(
length = length(obj),
class = "character",
mode = mode(obj)
)
} else if (is.numeric(obj)) {
summary_values <- as.list(summary(obj))
names(summary_values) <- tolower(gsub("\\.", "", names(summary_values)))
summary_values
} else if (is.logical(obj)) {
list(
mode = "logical",
"FALSE" = sum(!obj, na.rm = TRUE),
"TRUE" = sum(obj, na.rm = TRUE)
)
} else if (is.factor(obj)) {
summary_values <- summary(obj)
factor_list <- as.list(as.character(summary_values))
names(factor_list) <- names(summary_values)
factor_list
} else if (is.function(obj)) {
full_code <- paste(capture.output(obj), collapse = " ")
truncated_code <- substr(full_code, 1, func_char_limit)
if (nchar(full_code) > func_char_limit) {
truncated_code <- paste0(truncated_code, "...")
}
list(
code = truncated_code
)
} else if (is.ggplot(obj)) {
summarize_ggplot(obj)
} else {
list(summary = "Summary not available")
}
},
error = function(e) list(summary = "Summary not available")
)
# Add to summaries list
obj_summaries[[obj_name]] <- list(
type = obj_type,
summary = obj_summary,
size = obj_size
)
}
# Convert the list to YAML and return
as.yaml(obj_summaries)
}
# Generate the YAML summary with a customizable function character limit
environment_yaml <- summarize_environment_yaml(func_char_limit = 100) # Change character limit here
cat(environment_yaml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment