1
0
Fork 0
mirror of https://github.com/sensebox/opensensmapr synced 2025-02-18 17:23:57 +01:00
opensensmapR/R/box_utils.R

74 lines
2.2 KiB
R
Raw Permalink Normal View History

2017-08-13 16:07:24 +02:00
#' @export
plot.sensebox = function (x, ..., mar = c(2, 2, 1, 1)) {
2017-08-17 21:32:56 +02:00
geom = x %>%
sf::st_as_sf() %>%
sf::st_geometry()
2017-08-17 21:32:56 +02:00
bbox = sf::st_bbox(geom)
world = maps::map('world', plot = FALSE, fill = TRUE) %>%
sf::st_as_sf() %>%
sf::st_geometry()
2023-03-06 11:17:33 +01:00
oldpar <- par(no.readonly = TRUE)
on.exit(par(oldpar))
par(mar = mar)
2023-03-06 10:19:37 +01:00
plot(world, col = 'gray', xlim = bbox[c(1, 3)], ylim = bbox[c(2, 4)], axes = TRUE, ...)
plot(geom, add = TRUE, col = x$exposure, ...)
2017-08-17 21:32:56 +02:00
legend('left', legend = levels(x$exposure), col = 1:length(x$exposure), pch = 1)
2017-08-10 18:01:09 +02:00
invisible(x)
}
2017-08-13 16:07:24 +02:00
#' @export
2018-05-08 00:52:20 +02:00
print.sensebox = function(x, columns = c('name', 'exposure', 'lastMeasurement', 'phenomena'), ...) {
2017-08-14 18:10:16 +02:00
data = as.data.frame(x)
2018-05-08 00:52:20 +02:00
print(dplyr::select(data, dplyr::one_of(columns)), ...)
2017-08-10 18:01:09 +02:00
invisible(x)
}
2017-08-13 16:07:24 +02:00
#' @export
summary.sensebox = function(object, ...) {
2023-03-06 10:19:37 +01:00
cat('boxes total:', nrow(object), fill = TRUE)
2017-08-10 18:01:09 +02:00
cat('\nboxes by exposure:')
2017-08-13 16:07:24 +02:00
table(object$exposure) %>% print()
2017-08-10 18:01:09 +02:00
cat('\nboxes by model:')
2017-08-13 16:07:24 +02:00
table(object$model) %>% print()
2017-08-10 18:01:09 +02:00
cat('\n')
2019-02-09 22:10:25 +01:00
diffNow = (date_as_utc(Sys.time()) - object$lastMeasurement) %>% as.numeric(unit = 'hours')
2017-08-10 18:01:09 +02:00
list(
'last_measurement_within' = c(
'1h' = nrow(dplyr::filter(object, diffNow <= 1)),
'1d' = nrow(dplyr::filter(object, diffNow <= 24)),
'30d' = nrow(dplyr::filter(object, diffNow <= 720)),
'365d' = nrow(dplyr::filter(object, diffNow <= 8760)),
'never' = nrow(dplyr::filter(object, is.na(lastMeasurement)))
2017-08-10 18:01:09 +02:00
)
2017-08-14 18:10:16 +02:00
) %>% print()
2017-08-10 18:01:09 +02:00
2017-08-13 16:07:24 +02:00
oldest = object[object$createdAt == min(object$createdAt), ]
newest = object[object$createdAt == max(object$createdAt), ]
2023-03-06 10:19:37 +01:00
cat('oldest box:', format(oldest$createdAt, '%F %T'), paste0('(', oldest$name, ')'), fill = TRUE)
cat('newest box:', format(newest$createdAt, '%F %T'), paste0('(', newest$name, ')'), fill = TRUE)
2017-08-10 18:01:09 +02:00
2023-03-06 10:19:37 +01:00
cat('\nsensors per box:', fill = TRUE)
2017-08-13 16:07:24 +02:00
lapply(object$phenomena, length) %>%
2017-08-10 18:01:09 +02:00
as.numeric() %>%
summary() %>%
print()
2017-08-13 16:07:24 +02:00
invisible(object)
2017-08-10 18:01:09 +02:00
}
#' Converts a foreign object to a sensebox data.frame.
#' @param x A data.frame to attach the class to
2023-03-06 11:04:40 +01:00
#' @return data.frame of class \code{sensebox}
#' @export
osem_as_sensebox = function(x) {
ret = as.data.frame(x)
class(ret) = c('sensebox', class(x))
ret
}