Skip to content

Commit

Permalink
support date columns
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Sep 15, 2024
1 parent 2e7d650 commit 2c8a6f4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
21 changes: 17 additions & 4 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ target_get_dataset <- function(name, req) {
variables <- list()
for (col in cols) {
lvls <- unique(dat[, col])
if (length(lvls) < 12) {
if (length(lvls) < 25) {
variables[[col]] <- list(name = jsonlite::unbox(col), levels = lvls)
}
}
Expand Down Expand Up @@ -100,6 +100,7 @@ target_get_trace <- function(name,
dataset <- read_dataset(req, name, scale)
dat <- dataset$data
xcol <- dataset$xcol
xtype <- dataset$xtype
cols <- colnames(dat)
if (!is.null(filter)) {
filters <- strsplit(filter, "+", fixed = TRUE)[[1]]
Expand All @@ -117,6 +118,7 @@ target_get_trace <- function(name,
return(lapply(seq_along(groups), function(i) {
model <- with_warnings(model_out(groups[[i]],
xcol = xcol,
xtype = xtype,
method = method,
span = span,
k = k))
Expand All @@ -129,6 +131,7 @@ target_get_trace <- function(name,
logger::log_info("Returning single trace")
model <- with_warnings(model_out(dat,
xcol = xcol,
xtype = xtype,
method = method,
span = span,
k = k))
Expand Down Expand Up @@ -156,11 +159,18 @@ read_dataset <- function(req, name, scale) {
if (scale == "log2") {
dat$value <- log2(dat$value)
}
if (all(is.na(as.numeric(dat[, xcol])))) {
xtype <- "date"
dat[, xcol] <- as.Date(lubridate::parse_date_time(dat[, xcol],
c("dmy", "mdy", "ymd", "ydm")))
} else {
xtype <- "number"
}
xcol <- readLines(file.path(path, "xcol"))
list(data = dat, xcol = xcol)
list(data = dat, xcol = xcol, xtype = xtype)
}

model_out <- function(dat, xcol, method = "auto", span = 0.75, k = 10) {
model_out <- function(dat, xcol, xtype, method = "auto", span = 0.75, k = 10) {
n <- nrow(dat)
if (n == 0) {
return(list(x = list(), y = list()))
Expand All @@ -170,13 +180,16 @@ model_out <- function(dat, xcol, method = "auto", span = 0.75, k = 10) {
m <- mgcv::gam(eval(parse(text = fmla)),
data = dat, method = "REML")
} else {
fmla <- sprintf("value ~ %s", xcol)
fmla <- sprintf("value ~ as.numeric(%s)", xcol)
m <- stats::loess(fmla, data = dat, span = span)
}
range <- range(dat[, xcol], na.rm = TRUE)
xseq <- range[1]:range[2]
xdf <- tibble::tibble(xcol = xseq)
names(xdf) <- xcol
if (xtype == "date") {
xseq <- format(as.Date(xseq, origin = "1970-01-01"))
}
list(x = xseq, y = stats::predict(m, xdf))
}

Expand Down
4 changes: 2 additions & 2 deletions inst/schema/DataSeries.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"x": {
"type": "array",
"items": {
"type": "number"
"type": ["number", "string"]
}
},
"y": {
Expand All @@ -32,7 +32,7 @@
"x": {
"type": "array",
"items": {
"type": "number"
"type": ["number", "string"]
}
},
"y": {
Expand Down
2 changes: 1 addition & 1 deletion inst/schema/Variable.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"levels": {
"type": "array",
"items": {
"type": "string"
"type": ["string", "number", "null"]
}
}
},
Expand Down

0 comments on commit 2c8a6f4

Please sign in to comment.