-- [[ Rscripts Risk Notice ]] -- This script is not verified by rscripts.net. Deal with caution. -- -- Stay safe: -- • Never log in on unofficial Roblox sites or lookalike domains. -- • Real Roblox links use roblox.com (check the .com ending). -- • Treat fake Roblox login / "claim reward" pages as phishing. -- [[ End Rscripts Risk Notice ]] #!/usr/bin/env Rscript # ============================================================================= # analysis_pipeline.R — Core analysis functions (reusable by CLI or GUI) # ============================================================================= suppressPackageStartupMessages({ library(dplyr) library(ggplot2) library(readr) library(tidyr) library(stringr) }) # ---- Config (overridable by caller) ---- default_config <- list( output_dir = "output", plot_dir = "output/plots", seed = 42, missing_threshold = 0.5 ) #' Log helper log_msg <- function(msg, level = "INFO") { cat(sprintf("[%s] %-5s %s\n", Sys.time(), level, msg)) } #' Load a CSV from a path load_data <- function(path) { if (!file.exists(path)) stop("Input file not found: ", path, call. = FALSE) df <- readr::read_csv(path, show_col_types = FALSE) if (nrow(df) == 0) stop("Input file has no rows.", call. = FALSE) log_msg(sprintf("Loaded %d rows x %d cols", nrow(df), ncol(df))) df } #' Clean a dataframe clean_data <- function(df, missing_threshold = 0.5) { miss_frac <- sapply(df, function(x) mean(is.na(x))) keep_cols <- names(miss_frac)[miss_frac <= missing_threshold] dropped <- setdiff(names(df), keep_cols) if (length(dropped)) log_msg(sprintf("Dropping sparse columns: %s", paste(dropped, collapse = ", ")), "WARN") df <- df[, keep_cols, drop = FALSE] df <- df %>% mutate(across(where(is.character), str_trim)) before <- nrow(df) df <- distinct(df) if (nrow(df) < before) log_msg(sprintf("Removed %d duplicate rows", before - nrow(df))) df } #' Numeric summary table summarize_numeric <- function(df) { num_cols <- names(df)[sapply(df, is.numeric)] if (!length(num_cols)) return(NULL) df %>% summarise(across(all_of(num_cols), list(mean = ~mean(.x, na.rm = TRUE), sd = ~sd(.x, na.rm = TRUE), min = ~min(.x, na.rm = TRUE), max = ~max(.x, na.rm = TRUE)))) %>% pivot_longer(everything(), names_to = c("variable", ".value"), names_sep = "_") } #' Build a histogram make_histogram <- function(df, col) { ggplot(df, aes(x = .data[[col]])) + geom_histogram(bins = 30, fill = "#3B7DD8", color = "white") + labs(title = paste("Distribution of", col), x = col, y = "Count") + theme_minimal(base_size = 12) } #' Save plot save_plot <- function(plot, filename, plot_dir, width = 8, height = 5) { dir.create(plot_dir, recursive = TRUE, showWarnings = FALSE) path <- file.path(plot_dir, filename) ggsave(path, plot, width = width, height = height, dpi = 300, bg = "white") log_msg(sprintf("Saved plot: %s", path)) path } #' Full pipeline (returns list of results) run_pipeline <- function(input_path, config = default_config) { set.seed(config$seed) dir.create(config$output_dir, recursive = TRUE, showWarnings = FALSE) raw <- load_data(input_path) clean <- clean_data(raw, config$missing_threshold) summ <- summarize_numeric(clean) results <- list(raw = raw, clean = clean, summary = summ) if (!is.null(summ)) { write_csv(summ, file.path(config$output_dir, "numeric_summary.csv")) } write_csv(clean, file.path(config$output_dir, "clean_data.csv")) num_cols <- names(clean)[sapply(clean, is.numeric)] if (length(num_cols) >= 1) { p <- make_histogram(clean, num_cols[1]) results$plot_path <- save_plot(p, "distribution.png", config$plot_dir) results$plot <- p } log_msg("Pipeline completed") results }