Skip to content

Commit

Permalink
add lintr
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Aug 14, 2024
1 parent 83ad743 commit 495b4d8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
^docker$
^\.github$
^\.idea$
^\uploads$
^\uploads$
^.lintr
2 changes: 2 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
linters: linters_with_defaults(object_usage_linter = NULL, indentation_linter = NULL, commented_code_linter = NULL)
encoding: "UTF-8"
20 changes: 13 additions & 7 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@ target_get_root <- function() {
}

target_get_version <- function() {
jsonlite::toJSON(as.character(utils::packageVersion("serovizr")), auto_unbox = TRUE)
jsonlite::toJSON(as.character(utils::packageVersion("serovizr")),
auto_unbox = TRUE)
}

target_post_dataset <- function(req, res) {
parsed <- Rook::Multipart$parse(req)
file_body <- utils::read.csv(parsed$file$tempfile)
filename <- parsed$file$filename
filename <- stringr::str_remove_all(filename, paste0(".", tools::file_ext(filename)))
filename <- stringr::str_remove_all(filename,
paste0(".", tools::file_ext(filename)))
path <- file.path("uploads", filename)
if (file.exists(path)) {
res$status <- 400L
error <- list(error = "BAD_REQUEST", detail = paste("A dataset called", filename,
"already exists. Please choose a unique name for this dataset."))
msg <- paste(filename, "already exists.",
"Please choose a unique name for this dataset.")
error <- list(error = "BAD_REQUEST",
detail = msg)
return(list(status = "failure", errors = list(error), data = NULL))
}
required_cols <- c("value", "biomarker")
missing_cols <- required_cols[!(required_cols %in% colnames(file_body))]
if (length(missing_cols) > 0) {
res$status <- 400L
error <- list(error = "BAD_REQUEST",
detail = paste("Missing required columns:", paste(missing_cols, collapse = ", ")))
detail = paste("Missing required columns:",
paste(missing_cols, collapse = ", ")))
return(list(status = "failure", errors = list(error), data = NULL))
}

Expand Down Expand Up @@ -51,7 +56,8 @@ target_get_datasets <- function() {
}

target_get_trace <- function(name, biomarker, facet = NULL, trace = NULL) {
logger::log_info(paste("Requesting data from", name, "with biomarker", biomarker))
logger::log_info(paste("Requesting data from", name,
"with biomarker", biomarker))
logger::log_info(paste("Filtering by facet variables", facet))
dat <- read_dataset(name)
cols <- colnames(dat)
Expand All @@ -63,7 +69,7 @@ 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))
Expand Down
27 changes: 16 additions & 11 deletions R/router.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ build_routes <- function() {

pr$handle(get_root())
pr$handle(get_version())
pr$handle("POST", "/dataset/", function(req, res) target_post_dataset(req, res),
pr$handle("POST", "/dataset/",
function(req, res) target_post_dataset(req, res),
serializer = plumber::serializer_unboxed_json())
pr$handle(get_dataset())
pr$handle(get_datasets())
Expand All @@ -16,17 +17,19 @@ build_routes <- function() {


get_root <- function() {
porcelain::porcelain_endpoint$new("GET",
"/",
target_get_root,
returning = porcelain::porcelain_returning_json())
porcelain::porcelain_endpoint$new(
"GET",
"/",
target_get_root,
returning = porcelain::porcelain_returning_json())
}

get_version <- function() {
porcelain::porcelain_endpoint$new("GET",
"/version",
target_get_version,
returning = porcelain::porcelain_returning_json("Version"))
porcelain::porcelain_endpoint$new(
"GET",
"/version",
target_get_version,
returning = porcelain::porcelain_returning_json("Version"))
}

get_dataset <- function() {
Expand All @@ -38,14 +41,16 @@ get_dataset <- function() {

get_datasets <- function() {
porcelain::porcelain_endpoint$new(
"GET", "/datasets/",
"GET",
"/datasets/",
target_get_datasets,
returning = porcelain::porcelain_returning_json("Datasets"))
}

get_trace <- function() {
porcelain::porcelain_endpoint$new(
"GET", "/dataset/<name>/<biomarker>/",
"GET",
"/dataset/<name>/<biomarker>/",
target_get_trace,
porcelain::porcelain_input_query(facet = "string", trace = "string"),
returning = porcelain::porcelain_returning_json())
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<!-- badges: start -->
[![Project Status: Concept – Minimal or no implementation has been done yet, or the repository is only intended to be a limited example, demo, or proof-of-concept.](https://www.repostatus.org/badges/latest/concept.svg)](https://www.repostatus.org/#concept)
[![R-CMD-check.yaml](https://github.com/seroanalytics/serovizr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/seroanalytics/serovizr/actions/workflows/R-CMD-check.yaml)[![Codecov test coverage](https://codecov.io/gh/seroanalytics/serovizr/branch/master/graph/badge.svg)](https://codecov.io/gh/seroanalytics/serovizr?branch=master)
[![R-CMD-check.yaml](https://github.com/seroanalytics/serovizr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/seroanalytics/serovizr/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/seroanalytics/serovizr/branch/master/graph/badge.svg)](https://codecov.io/gh/seroanalytics/serovizr?branch=master)
<!-- badges: end -->

R API for the SeroViz app. Based on the [porcelain](https://github.com/reside-ic/porcelain) framework.
Expand Down

0 comments on commit 495b4d8

Please sign in to comment.