Spaces:
Sleeping

File size: 1,169 Bytes
4b88607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env Rscript

args <- commandArgs(trailingOnly = TRUE)

if (length(args) < 1) {
  cat("EQUIL2 CLI - Batch Urinary Supersaturation Calculator\n")
  cat("Usage: Rscript run_equil2_cli.R input.csv [output.csv]\n")
  quit(status = 1)
}

suppressPackageStartupMessages({
  library(readr)
  library(jsonlite)
  library(dplyr)
})

source("helpers.R")

input_file <- args[1]
if (!file.exists(input_file)) {
  cat("Error: Input file not found.\n")
  quit(status = 1)
}

output_file <- if (length(args) >= 2) args[2] else NULL
output_to_csv <- !is.null(output_file) && grepl("\\.csv$", output_file, ignore.case = TRUE)

tryCatch({
  df_input <- readr::read_csv(input_file, show_col_types = FALSE)
  df_converted <- convert_and_validate(df_input)
  results <- run_equil2_dataframe(df_converted)
  
  if (output_to_csv) {
    output_df <- as.data.frame(results$records)
    readr::write_csv(output_df, output_file)
    cat("Results written to:", output_file, "\n")
  } else {
    json_output <- jsonlite::toJSON(results, auto_unbox = TRUE, pretty = TRUE)
    cat(json_output)
  }
}, error = function(e) {
  cat("Error:", conditionMessage(e), "\n")
  quit(status = 1)
})