From 79b4427ae531e5a3fcd0f11d35fa1e13e20731c7 Mon Sep 17 00:00:00 2001 From: "alex.hill@gmail.com" Date: Fri, 16 Aug 2024 17:22:36 +0100 Subject: [PATCH] add schemas --- R/api.R | 16 ++++--- R/router.R | 6 +-- inst/schema/DataSeries.schema.json | 46 +++++++++++++++++++ ...chema.json => DatasetMetadata.schema.json} | 0 ...s.schema.json => DatasetNames.schema.json} | 0 5 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 inst/schema/DataSeries.schema.json rename inst/schema/{Data.schema.json => DatasetMetadata.schema.json} (100%) rename inst/schema/{Datasets.schema.json => DatasetNames.schema.json} (100%) diff --git a/R/api.R b/R/api.R index 1fbbc3e..97f893c 100644 --- a/R/api.R +++ b/R/api.R @@ -69,20 +69,22 @@ target_get_trace <- function(name, biomarker, facet = NULL, trace = NULL) { # code = "BAD_REQUEST", status_code = 400L) # } # dat <- dat[dat[facet_var] == facet_level & dat["biomarker"] == biomarker,] - dat <- dat[dat["biomarker"] == biomarker, ] + dat <- dat[dat["biomarker"] == biomarker,] dat$value <- log(dat$value) if (length(trace) > 0) { logger::log_info(paste("Disaggregating by trace variables", trace)) groups <- split(dat, eval(parse(text = paste("~", trace)))) - model_result <- lapply(groups, model_out) - raw <- lapply(groups, data_out) + return(lapply(seq_along(groups), function(i) { + list(name = jsonlite::unbox(nms[[g]]), + model = model_out(groups[[i]]), + raw = data_out(groups[[i]])) + })) } else { logger::log_info("Returning single trace") - browser() - model_result <- list(all = model_out(dat)) - raw <- list(all = data_out(dat)) + return(list(list(name = jsonlite::unbox("all"), + model = model_out(dat), + raw = data_out(dat)))) } - list(model = model_result, raw = raw) } read_dataset <- function(name) { diff --git a/R/router.R b/R/router.R index c547c7c..1135732 100644 --- a/R/router.R +++ b/R/router.R @@ -36,7 +36,7 @@ get_dataset <- function() { porcelain::porcelain_endpoint$new( "GET", "/dataset/", target_get_dataset, - returning = porcelain::porcelain_returning_json()) + returning = porcelain::porcelain_returning_json("DatasetMetadata")) } get_datasets <- function() { @@ -44,7 +44,7 @@ get_datasets <- function() { "GET", "/datasets/", target_get_datasets, - returning = porcelain::porcelain_returning_json("Datasets")) + returning = porcelain::porcelain_returning_json("DatasetNames")) } get_trace <- function() { @@ -53,5 +53,5 @@ get_trace <- function() { "/dataset///", target_get_trace, porcelain::porcelain_input_query(facet = "string", trace = "string"), - returning = porcelain::porcelain_returning_json()) + returning = porcelain::porcelain_returning_json("DataSeries")) } diff --git a/inst/schema/DataSeries.schema.json b/inst/schema/DataSeries.schema.json new file mode 100644 index 0000000..2c60bef --- /dev/null +++ b/inst/schema/DataSeries.schema.json @@ -0,0 +1,46 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "model": { + "type": "object", + "properties": { + "x": { + "type": "array", + "items": { + "type": "number" + } + }, + "y": { + "type": "array", + "items": { + "type": ["number", "null"] + } + } + } + }, + "raw": { + "type": "object", + "properties": { + "x": { + "type": "array", + "items": { + "type": "number" + } + }, + "y": { + "type": "array", + "items": { + "type": ["number", "null"] + } + } + } + } + } + } +} diff --git a/inst/schema/Data.schema.json b/inst/schema/DatasetMetadata.schema.json similarity index 100% rename from inst/schema/Data.schema.json rename to inst/schema/DatasetMetadata.schema.json diff --git a/inst/schema/Datasets.schema.json b/inst/schema/DatasetNames.schema.json similarity index 100% rename from inst/schema/Datasets.schema.json rename to inst/schema/DatasetNames.schema.json