Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Aug 13, 2024
1 parent 2fd0b19 commit a65aa0b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
34 changes: 28 additions & 6 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,25 @@ target_post_dataset <- function(req, res) {
target_get_dataset <- function(name) {
dat <- read_dataset(name)
cols <- setdiff(colnames(dat), c("value", "biomarker", "day"))
list(variables = cols, data = jsonlite::toJSON(dat))
biomarkers <- unique(dat$biomarker)
variables <- list()
for (col in cols) {
lvls <- unique(dat[, col])
if (length(lvls) < 12) {
variables[[col]] <- list(name = jsonlite::unbox(col), levels = lvls)
}
}
list(variables = unname(variables), biomarkers = biomarkers)
}

target_get_datasets <- function() {
files <- list.files("uploads")
jsonlite::toJSON(files)
}

target_get_trace <- function(name, biomarker, facet, trace = "age") {
target_get_trace <- function(name, biomarker, facet = NULL, trace = NULL) {
logger::log_info(paste("Requesting data from", name, "with biomarker", biomarker))

Check warning on line 54 in R/api.R

View workflow job for this annotation

GitHub Actions / lint

file=R/api.R,line=54,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 84 characters.
logger::log_info(paste("Filtering by facet variables", facet))
dat <- read_dataset(name)
cols <- colnames(dat)

Check warning on line 57 in R/api.R

View workflow job for this annotation

GitHub Actions / lint

file=R/api.R,line=57,col=3,[object_usage_linter] local variable 'cols' assigned but may not be used
# facet_def <- strsplit(facet, ":")

Check warning on line 58 in R/api.R

View workflow job for this annotation

GitHub Actions / lint

file=R/api.R,line=58,col=5,[commented_code_linter] Commented code should be removed.
Expand All @@ -55,9 +65,17 @@ target_get_trace <- function(name, biomarker, facet, trace = "age") {
# dat <- dat[dat[facet_var] == facet_level & dat["biomarker"] == biomarker,]
dat <- dat[dat["biomarker"] == biomarker,]
dat$value <- log(dat$value)
groups <- split(dat, eval(parse(text = paste("~", trace))))
model_result <- lapply(groups, model_out)
raw <- lapply(groups, data_out)
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)
} else {
logger::log_info("Returning single trace")
browser()
model_result <- list(all = model_out(dat))
raw <- list(all = data_out(dat))
}
list(model = model_result, raw = raw)
}

Expand All @@ -73,7 +91,11 @@ read_dataset <- function(name) {
}

model_out <- function(dat) {
if (nrow(dat) > 1000) {
n <- nrow(dat)
if (n == 0) {
return(list(x = list(), y = list()))
}
if (n > 1000) {
m <- mgcv::gam(value ~ s(day, bs = "cs"), data = dat, method = "REML")
} else {
m <- stats::loess(value ~ day, data = dat, span = 0.75)
Expand Down
3 changes: 2 additions & 1 deletion R/router.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ get_datasets <- function() {

get_trace <- function() {
porcelain::porcelain_endpoint$new(
"GET", "/dataset/<name>/<biomarker>/<facet>",
"GET", "/dataset/<name>/<biomarker>/",
target_get_trace,
porcelain::porcelain_input_query(facet = "string", trace = "string"),
returning = porcelain::porcelain_returning_json())
}
10 changes: 8 additions & 2 deletions inst/schema/Data.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"type": "object",
"properties": {
"variables": {
"type": "array",
"items": {
"type": "object"
}
},
"biomarkers": {
"type": "array",
"items": {
"type": "string"
Expand Down Expand Up @@ -30,7 +36,7 @@
},
"additionalProperties": false,
"required": [
"data",
"variables"
"variables",
"biomarkers"
]
}

0 comments on commit a65aa0b

Please sign in to comment.