export util functions

pull/17/head
noerw 7 years ago
parent 53b77977e0
commit 79057d802b

@ -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)

@ -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()

@ -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)
}

@ -1,3 +1,4 @@
#' @export
plot.osem_measurements = function (x, ...) {
# TODO: group/color by sensor_id
plot(value~createdAt, x, ...)

@ -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)
}
Loading…
Cancel
Save