DSP Project first push, date: 29/01/2026
This commit is contained in:
18
r_scripts/.ipynb_checkpoints/data_summary-checkpoint.R
Normal file
18
r_scripts/.ipynb_checkpoints/data_summary-checkpoint.R
Normal file
@@ -0,0 +1,18 @@
|
||||
# r_scripts/data_summary.R
|
||||
# This script is a simple version for testing the PHP runner.
|
||||
|
||||
args <- commandArgs(trailingOnly = TRUE)
|
||||
data_file_path <- args[1]
|
||||
params_json <- args[2]
|
||||
|
||||
# Just print the received arguments to confirm they are passed correctly.
|
||||
cat(jsonlite::toJSON(
|
||||
list(
|
||||
message = "R script ran successfully!",
|
||||
data_file = data_file_path,
|
||||
params_received = params_json
|
||||
),
|
||||
pretty = TRUE,
|
||||
auto_unbox = TRUE
|
||||
))
|
||||
|
||||
79
r_scripts/category_frequency.R
Normal file
79
r_scripts/category_frequency.R
Normal file
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env Rscript
|
||||
|
||||
`%||%` <- function(lhs, rhs) {
|
||||
if (is.null(lhs) || is.na(lhs)) rhs else lhs
|
||||
}
|
||||
|
||||
suppressPackageStartupMessages({
|
||||
library(jsonlite)
|
||||
library(readr)
|
||||
})
|
||||
|
||||
args <- commandArgs(trailingOnly = TRUE)
|
||||
|
||||
if (length(args) < 2) {
|
||||
stop("Expected arguments: <data_file_path> <params_json>")
|
||||
}
|
||||
|
||||
data_file_path <- args[1]
|
||||
params_json <- args[2]
|
||||
|
||||
if (!file.exists(data_file_path)) {
|
||||
stop(sprintf("Data file not found at %s", data_file_path))
|
||||
}
|
||||
|
||||
params <- tryCatch(
|
||||
fromJSON(params_json),
|
||||
error = function(e) list()
|
||||
)
|
||||
|
||||
target_column <- params$column
|
||||
if (is.null(target_column) || target_column == "") {
|
||||
stop("Parameter 'column' is required for category_frequency.R")
|
||||
}
|
||||
|
||||
top_n <- as.integer(params$top_n %||% 10)
|
||||
include_missing <- isTRUE(params$include_missing)
|
||||
|
||||
data <- tryCatch(
|
||||
read_csv(
|
||||
file = data_file_path,
|
||||
show_col_types = FALSE,
|
||||
progress = FALSE,
|
||||
locale = locale(encoding = params$encoding %||% "UTF-8")
|
||||
),
|
||||
error = function(e) stop(sprintf("Failed to read CSV: %s", e$message))
|
||||
)
|
||||
|
||||
if (!target_column %in% names(data)) {
|
||||
stop(sprintf("Column '%s' not found in dataset", target_column))
|
||||
}
|
||||
|
||||
column_vector <- data[[target_column]]
|
||||
|
||||
if (!include_missing) {
|
||||
column_vector <- column_vector[!is.na(column_vector)]
|
||||
}
|
||||
|
||||
frequency_table <- sort(table(column_vector, useNA = if (include_missing) "always" else "no"), decreasing = TRUE)
|
||||
|
||||
freq_df <- head(as.data.frame(frequency_table, stringsAsFactors = FALSE), top_n)
|
||||
names(freq_df) <- c("value", "count")
|
||||
|
||||
output <- list(
|
||||
message = sprintf("Top %s frequency distribution for '%s'.", top_n, target_column),
|
||||
analyzed_column = target_column,
|
||||
top_n = top_n,
|
||||
include_missing = include_missing,
|
||||
frequencies = freq_df
|
||||
)
|
||||
|
||||
cat(
|
||||
toJSON(
|
||||
output,
|
||||
pretty = TRUE,
|
||||
auto_unbox = TRUE,
|
||||
na = "null",
|
||||
dataframe = "rows"
|
||||
)
|
||||
)
|
||||
18
r_scripts/data_summary.R
Normal file
18
r_scripts/data_summary.R
Normal file
@@ -0,0 +1,18 @@
|
||||
# r_scripts/data_summary.R
|
||||
# This script is a simple version for testing the PHP runner.
|
||||
|
||||
args <- commandArgs(trailingOnly = TRUE)
|
||||
data_file_path <- args[1]
|
||||
params_json <- args[2]
|
||||
|
||||
# Just print the received arguments to confirm they are passed correctly.
|
||||
cat(jsonlite::toJSON(
|
||||
list(
|
||||
message = "R script ran successfully!",
|
||||
data_file = data_file_path,
|
||||
params_received = params_json
|
||||
),
|
||||
pretty = TRUE,
|
||||
auto_unbox = TRUE
|
||||
))
|
||||
|
||||
83
r_scripts/descriptive_stats.R
Normal file
83
r_scripts/descriptive_stats.R
Normal file
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env Rscript
|
||||
|
||||
`%||%` <- function(lhs, rhs) {
|
||||
if (is.null(lhs) || is.na(lhs)) rhs else lhs
|
||||
}
|
||||
|
||||
suppressPackageStartupMessages({
|
||||
library(jsonlite)
|
||||
library(readr)
|
||||
library(dplyr)
|
||||
})
|
||||
|
||||
args <- commandArgs(trailingOnly = TRUE)
|
||||
|
||||
if (length(args) < 2) {
|
||||
stop("Expected arguments: <data_file_path> <params_json>")
|
||||
}
|
||||
|
||||
data_file_path <- args[1]
|
||||
params_json <- args[2]
|
||||
|
||||
if (!file.exists(data_file_path)) {
|
||||
stop(sprintf("Data file not found at %s", data_file_path))
|
||||
}
|
||||
|
||||
params <- tryCatch(
|
||||
fromJSON(params_json),
|
||||
error = function(e) list()
|
||||
)
|
||||
|
||||
read_opts <- list(
|
||||
locale = locale(encoding = params$encoding %||% "UTF-8")
|
||||
)
|
||||
|
||||
data <- tryCatch(
|
||||
read_csv(
|
||||
file = data_file_path,
|
||||
show_col_types = FALSE,
|
||||
progress = FALSE,
|
||||
guess_max = params$guess_max %||% 1000,
|
||||
locale = read_opts$locale
|
||||
),
|
||||
error = function(e) stop(sprintf("Failed to read CSV: %s", e$message))
|
||||
)
|
||||
|
||||
numeric_columns <- select(data, where(is.numeric))
|
||||
|
||||
column_stats <- lapply(numeric_columns, function(column) {
|
||||
list(
|
||||
count = sum(!is.na(column)),
|
||||
mean = mean(column, na.rm = TRUE),
|
||||
median = median(column, na.rm = TRUE),
|
||||
sd = sd(column, na.rm = TRUE),
|
||||
min = min(column, na.rm = TRUE),
|
||||
max = max(column, na.rm = TRUE),
|
||||
missing = sum(is.na(column))
|
||||
)
|
||||
})
|
||||
|
||||
message_text <- if (length(column_stats) > 0) {
|
||||
"Descriptive statistics generated successfully."
|
||||
} else {
|
||||
"No numeric columns detected; returning dataset preview only."
|
||||
}
|
||||
|
||||
preview_rows <- head(data, n = min(5, nrow(data)))
|
||||
|
||||
output <- list(
|
||||
message = message_text,
|
||||
numeric_columns = column_stats,
|
||||
sample_rows = preview_rows,
|
||||
params = params
|
||||
)
|
||||
|
||||
cat(
|
||||
toJSON(
|
||||
output,
|
||||
pretty = TRUE,
|
||||
auto_unbox = TRUE,
|
||||
na = "null",
|
||||
dataframe = "rows"
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user