mirror of
https://github.com/sensebox/opensensmapr
synced 2025-02-21 13:23:57 +01:00
export util functions
This commit is contained in:
parent
53b77977e0
commit
79057d802b
5 changed files with 34 additions and 19 deletions
|
@ -4,6 +4,11 @@ S3method(osem_measurements,bbox)
|
||||||
S3method(osem_measurements,default)
|
S3method(osem_measurements,default)
|
||||||
S3method(osem_measurements,sensebox)
|
S3method(osem_measurements,sensebox)
|
||||||
S3method(osem_phenomena,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_box)
|
||||||
export(osem_boxes)
|
export(osem_boxes)
|
||||||
export(osem_counts)
|
export(osem_counts)
|
||||||
|
|
1
R/api.R
1
R/api.R
|
@ -33,7 +33,6 @@ get_box_ = function (..., endpoint) {
|
||||||
}
|
}
|
||||||
|
|
||||||
get_measurements_ = function (..., endpoint) {
|
get_measurements_ = function (..., endpoint) {
|
||||||
# FIXME: get rid of readr warnings
|
|
||||||
result = httr::GET(endpoint, path = c('boxes', 'data'), query = list(...)) %>%
|
result = httr::GET(endpoint, path = c('boxes', 'data'), query = list(...)) %>%
|
||||||
httr::content(encoding = 'UTF-8') %>%
|
httr::content(encoding = 'UTF-8') %>%
|
||||||
osem_remote_error()
|
osem_remote_error()
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#' @export
|
||||||
plot.sensebox = function (x, ...) {
|
plot.sensebox = function (x, ...) {
|
||||||
# TODO: background map (maps::world), graticule?
|
# TODO: background map (maps::world), graticule?
|
||||||
geom = x %>%
|
geom = x %>%
|
||||||
|
@ -10,6 +11,7 @@ plot.sensebox = function (x, ...) {
|
||||||
invisible(x)
|
invisible(x)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#' @export
|
||||||
print.sensebox = function(x, ...) {
|
print.sensebox = function(x, ...) {
|
||||||
important_columns = c('name', 'exposure', 'lastMeasurement', 'phenomena')
|
important_columns = c('name', 'exposure', 'lastMeasurement', 'phenomena')
|
||||||
data = as.data.frame(x) # to get rid of the sf::`<-[` override..
|
data = as.data.frame(x) # to get rid of the sf::`<-[` override..
|
||||||
|
@ -18,37 +20,38 @@ print.sensebox = function(x, ...) {
|
||||||
invisible(x)
|
invisible(x)
|
||||||
}
|
}
|
||||||
|
|
||||||
summary.sensebox = function(x, ...) {
|
#' @export
|
||||||
cat('boxes total:', nrow(x), fill = T)
|
summary.sensebox = function(object, ...) {
|
||||||
|
cat('box total:', nrow(object), fill = T)
|
||||||
cat('\nboxes by exposure:')
|
cat('\nboxes by exposure:')
|
||||||
table(x$exposure) %>% print()
|
table(object$exposure) %>% print()
|
||||||
cat('\nboxes by model:')
|
cat('\nboxes by model:')
|
||||||
table(x$model) %>% print()
|
table(object$model) %>% print()
|
||||||
cat('\n')
|
cat('\n')
|
||||||
|
|
||||||
diffNow = (utc_date(Sys.time()) - x$lastMeasurement) %>% as.numeric(unit='hours')
|
diffNow = (utc_date(Sys.time()) - object$lastMeasurement) %>% as.numeric(unit='hours')
|
||||||
neverActive = x[is.na(x$lastMeasurement), ] %>% nrow()
|
neverActive = object[is.na(object$lastMeasurement), ] %>% nrow()
|
||||||
list(
|
list(
|
||||||
'last_measurement_within' = c(
|
'last_measurement_within' = c(
|
||||||
'1h' = nrow(x[diffNow <= 1, ]) - neverActive,
|
'1h' = nrow(object[diffNow <= 1, ]) - neverActive,
|
||||||
'1d' = nrow(x[diffNow <= 24, ]) - neverActive,
|
'1d' = nrow(object[diffNow <= 24, ]) - neverActive,
|
||||||
'30d' = nrow(x[diffNow <= 720, ]) - neverActive,
|
'30d' = nrow(object[diffNow <= 720, ]) - neverActive,
|
||||||
'365d' = nrow(x[diffNow <= 8760, ]) - neverActive,
|
'365d' = nrow(object[diffNow <= 8760, ]) - neverActive,
|
||||||
'never' = neverActive
|
'never' = neverActive
|
||||||
)
|
)
|
||||||
) %>%
|
) %>%
|
||||||
print()
|
print()
|
||||||
|
|
||||||
oldest = x[x$createdAt == min(x$createdAt), ]
|
oldest = object[object$createdAt == min(object$createdAt), ]
|
||||||
newest = x[x$createdAt == max(x$createdAt), ]
|
newest = object[object$createdAt == max(object$createdAt), ]
|
||||||
cat('oldest box:', format(oldest$createdAt, '%F %T'), paste0('(', oldest$name, ')'), fill = T)
|
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('newest box:', format(newest$createdAt, '%F %T'), paste0('(', newest$name, ')'), fill = T)
|
||||||
|
|
||||||
cat('\nsensors per box:', fill = T)
|
cat('\nsensors per box:', fill = T)
|
||||||
lapply(x$phenomena, length) %>%
|
lapply(object$phenomena, length) %>%
|
||||||
as.numeric() %>%
|
as.numeric() %>%
|
||||||
summary() %>%
|
summary() %>%
|
||||||
print()
|
print()
|
||||||
|
|
||||||
invisible(x)
|
invisible(object)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#' @export
|
||||||
plot.osem_measurements = function (x, ...) {
|
plot.osem_measurements = function (x, ...) {
|
||||||
# TODO: group/color by sensor_id
|
# TODO: group/color by sensor_id
|
||||||
plot(value~createdAt, x, ...)
|
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) {
|
osem_remote_error = function (response) {
|
||||||
suppressWarnings({
|
suppressWarnings({
|
||||||
hasCode = !is.null(response$code)
|
hasCode = !is.null(response$code)
|
||||||
|
@ -25,7 +36,3 @@ utc_date = function (date) {
|
||||||
|
|
||||||
# NOTE: cannot handle mixed vectors of POSIXlt and POSIXct
|
# NOTE: cannot handle mixed vectors of POSIXlt and POSIXct
|
||||||
date_as_isostring = function (date) format(date, format = '%FT%TZ')
|
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…
Add table
Reference in a new issue