diff --git a/NAMESPACE b/NAMESPACE index db4db95..62e8b3e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,11 @@ S3method(osem_measurements,bbox) S3method(osem_measurements,default) S3method(osem_measurements,sensebox) S3method(osem_phenomena,sensebox) +S3method(plot,osem_measurements) +S3method(plot,sensebox) +S3method(print,sensebox) +S3method(summary,sensebox) +export(osem_as_sf) export(osem_box) export(osem_boxes) export(osem_counts) diff --git a/R/api.R b/R/api.R index 78c98fa..bad0c29 100644 --- a/R/api.R +++ b/R/api.R @@ -33,7 +33,6 @@ get_box_ = function (..., endpoint) { } get_measurements_ = function (..., endpoint) { - # FIXME: get rid of readr warnings result = httr::GET(endpoint, path = c('boxes', 'data'), query = list(...)) %>% httr::content(encoding = 'UTF-8') %>% osem_remote_error() diff --git a/R/box_utils.R b/R/box_utils.R index 2b6f3a2..41cf0ce 100644 --- a/R/box_utils.R +++ b/R/box_utils.R @@ -1,3 +1,4 @@ +#' @export plot.sensebox = function (x, ...) { # TODO: background map (maps::world), graticule? geom = x %>% @@ -10,6 +11,7 @@ plot.sensebox = function (x, ...) { invisible(x) } +#' @export print.sensebox = function(x, ...) { important_columns = c('name', 'exposure', 'lastMeasurement', 'phenomena') data = as.data.frame(x) # to get rid of the sf::`<-[` override.. @@ -18,37 +20,38 @@ print.sensebox = function(x, ...) { invisible(x) } -summary.sensebox = function(x, ...) { - cat('boxes total:', nrow(x), fill = T) +#' @export +summary.sensebox = function(object, ...) { + cat('box total:', nrow(object), fill = T) cat('\nboxes by exposure:') - table(x$exposure) %>% print() + table(object$exposure) %>% print() cat('\nboxes by model:') - table(x$model) %>% print() + table(object$model) %>% print() cat('\n') - diffNow = (utc_date(Sys.time()) - x$lastMeasurement) %>% as.numeric(unit='hours') - neverActive = x[is.na(x$lastMeasurement), ] %>% nrow() + diffNow = (utc_date(Sys.time()) - object$lastMeasurement) %>% as.numeric(unit='hours') + neverActive = object[is.na(object$lastMeasurement), ] %>% nrow() list( 'last_measurement_within' = c( - '1h' = nrow(x[diffNow <= 1, ]) - neverActive, - '1d' = nrow(x[diffNow <= 24, ]) - neverActive, - '30d' = nrow(x[diffNow <= 720, ]) - neverActive, - '365d' = nrow(x[diffNow <= 8760, ]) - neverActive, + '1h' = nrow(object[diffNow <= 1, ]) - neverActive, + '1d' = nrow(object[diffNow <= 24, ]) - neverActive, + '30d' = nrow(object[diffNow <= 720, ]) - neverActive, + '365d' = nrow(object[diffNow <= 8760, ]) - neverActive, 'never' = neverActive ) ) %>% print() - oldest = x[x$createdAt == min(x$createdAt), ] - newest = x[x$createdAt == max(x$createdAt), ] + oldest = object[object$createdAt == min(object$createdAt), ] + newest = object[object$createdAt == max(object$createdAt), ] cat('oldest box:', format(oldest$createdAt, '%F %T'), paste0('(', oldest$name, ')'), fill = T) cat('newest box:', format(newest$createdAt, '%F %T'), paste0('(', newest$name, ')'), fill = T) cat('\nsensors per box:', fill = T) - lapply(x$phenomena, length) %>% + lapply(object$phenomena, length) %>% as.numeric() %>% summary() %>% print() - invisible(x) + invisible(object) } diff --git a/R/measurement_utils.R b/R/measurement_utils.R index 33286b0..15757f0 100644 --- a/R/measurement_utils.R +++ b/R/measurement_utils.R @@ -1,3 +1,4 @@ +#' @export plot.osem_measurements = function (x, ...) { # TODO: group/color by sensor_id plot(value~createdAt, x, ...) diff --git a/R/api_utils.R b/R/utils.R similarity index 74% rename from R/api_utils.R rename to R/utils.R index ef59c82..b226733 100644 --- a/R/api_utils.R +++ b/R/utils.R @@ -1,3 +1,14 @@ +#' Convert a \code{sensebox} or \code{osem_measurements} dataframe to an +#' \code{\link[sf]{st_sf}} object. +#' +#' @param x The object to convert +#' @param ... maybe more objects to convert +#' @return The object with an st_geometry column attached. +#' @export +osem_as_sf = function (x, ...) { + sf::st_as_sf(x, ..., coords = c('lon', 'lat'), crs = 4326) +} + osem_remote_error = function (response) { suppressWarnings({ hasCode = !is.null(response$code) @@ -25,7 +36,3 @@ utc_date = function (date) { # NOTE: cannot handle mixed vectors of POSIXlt and POSIXct date_as_isostring = function (date) format(date, format = '%FT%TZ') - -osem_as_sf = function (x, ...) { - sf::st_as_sf(x, ..., coords = c('lon', 'lat'), crs = 4326) -}