meta-gpt / meta_analysis_api.R
mmrech's picture
Update meta_analysis_api.R
2499d49 verified
raw
history blame contribute delete
No virus
2.14 kB
library(plumber)
library(jsonlite)
# Load all the meta-analysis packages (as per your original code)
packages <- nstall.packages(c("nlme", "lme4", "GLMMadaptive", "glmmML", "glmmTMB", "MCMCglmm", "brms", "mbest", "survival", "CAMAN", "mclust", "flexclust", "aods3", "censReg", "betareg", "VGAM", "gee", "geepack", "glmtoolbox", "mgcv", "devtools", "remotes", "testthat", "covr", "Formula", "mathjaxr", "pander", "knitr", "rmarkdown", "tidyverse", "table1", "tableone"))
for (pkg in packages) {
suppressWarnings(suppressMessages(library(pkg, character.only = TRUE)))
}
#* @apiTitle Comprehensive Meta-Analysis API
#* Perform meta-analysis using the selected package and function
#* @param package The name of the package to use
#* @param function The name of the function within the package to execute
#* @param params A list containing all necessary parameters for the function
#* @post /analyze
function(package, function, params) {
# Check if the package is installed
if (!requireNamespace(package, quietly = TRUE)) {
res <- list(error = paste("Package", package, "is not installed."))
return(res)
}
# Load the package
suppressWarnings(suppressMessages(library(package, character.only = TRUE)))
# Check if the function exists in the package
if (!exists(function, where = asNamespace(package), mode = "function")) {
res <- list(error = paste("Function", function, "does not exist in package", package))
return(res)
}
# Convert params from JSON to R objects if necessary
params <- lapply(params, function(x) {
if (is.character(x)) {
# Attempt to parse JSON strings
parsed <- tryCatch(jsonlite::fromJSON(x), error = function(e) x)
return(parsed)
} else {
return(x)
}
})
# Perform the analysis by calling the specified function with the provided params
result <- tryCatch({
func <- get(function, envir = asNamespace(package))
do.call(func, params)
}, error = function(e) {
list(error = paste("Error in", package, "function", function, ":", e$message))
})
return(result)
}
#* @plumber
function(pr) {
pr$setDebug(TRUE)
}