mirror of
https://github.com/sensebox/opensensmapr
synced 2025-02-21 13:23:57 +01:00
fix lint
This commit is contained in:
parent
92cbbcbfc7
commit
32d0cceb28
4 changed files with 24 additions and 24 deletions
2
.lintr
2
.lintr
|
@ -1,4 +1,4 @@
|
||||||
exclusions: list('inst/doc/osem-intro.R')
|
exclusions: list.files(path = 'inst/doc', full.names = T)
|
||||||
linters: with_defaults(
|
linters: with_defaults(
|
||||||
# we use snake case
|
# we use snake case
|
||||||
camel_case_linter = NULL,
|
camel_case_linter = NULL,
|
||||||
|
|
38
R/archive.R
38
R/archive.R
|
@ -9,18 +9,18 @@ osem_archive_endpoint = function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
#' Fetch day-wise measurements for a single box from the openSenseMap archive.
|
#' Fetch day-wise measurements for a single box from the openSenseMap archive.
|
||||||
#'
|
#'
|
||||||
#' This function is significantly faster than \code{\link{osem_measurements}} for large
|
#' This function is significantly faster than \code{\link{osem_measurements}} for large
|
||||||
#' time-frames, as daily CSV dumps for each sensor from
|
#' time-frames, as daily CSV dumps for each sensor from
|
||||||
#' \href{http://archive.opensensemap.org}{archive.opensensemap.org} are used.
|
#' \href{http://archive.opensensemap.org}{archive.opensensemap.org} are used.
|
||||||
#' Note that the latest data available is from the previous day.
|
#' Note that the latest data available is from the previous day.
|
||||||
#'
|
#'
|
||||||
#' By default, data for all sensors of a box is fetched, but you can select a
|
#' By default, data for all sensors of a box is fetched, but you can select a
|
||||||
#' subset with a \code{\link[dplyr]{dplyr}}-style NSE filter expression.
|
#' subset with a \code{\link[dplyr]{dplyr}}-style NSE filter expression.
|
||||||
#'
|
#'
|
||||||
#' The function will warn when no data is available in the selected period,
|
#' The function will warn when no data is available in the selected period,
|
||||||
#' but continue the remaining download.
|
#' but continue the remaining download.
|
||||||
#'
|
#'
|
||||||
#' @param x A `sensebox data.frame` of a single box, as retrieved via \code{\link{osem_box}},
|
#' @param x A `sensebox data.frame` of a single box, as retrieved via \code{\link{osem_box}},
|
||||||
#' to download measurements for.
|
#' to download measurements for.
|
||||||
#' @param fromDate Start date for measurement download.
|
#' @param fromDate Start date for measurement download.
|
||||||
|
@ -28,11 +28,11 @@ osem_archive_endpoint = function () {
|
||||||
#' @param sensorFilter A NSE formula matching to \code{x$sensors}, selecting a subset of sensors.
|
#' @param sensorFilter A NSE formula matching to \code{x$sensors}, selecting a subset of sensors.
|
||||||
#' @param progress Whether to print download progress information, defaults to \code{TRUE}.
|
#' @param progress Whether to print download progress information, defaults to \code{TRUE}.
|
||||||
#' @return A \code{tbl_df} Containing observations of all selected sensors for each time stamp.
|
#' @return A \code{tbl_df} Containing observations of all selected sensors for each time stamp.
|
||||||
#'
|
#'
|
||||||
#' @seealso \href{https://archive.opensensemap.org}{openSenseMap archive}
|
#' @seealso \href{https://archive.opensensemap.org}{openSenseMap archive}
|
||||||
#' @seealso \code{\link{osem_measurements}}
|
#' @seealso \code{\link{osem_measurements}}
|
||||||
#' @seealso \code{\link{osem_box}}
|
#' @seealso \code{\link{osem_box}}
|
||||||
#'
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
osem_measurements_archive = function (x, ...) UseMethod('osem_measurements_archive')
|
osem_measurements_archive = function (x, ...) UseMethod('osem_measurements_archive')
|
||||||
|
|
||||||
|
@ -47,11 +47,11 @@ osem_measurements_archive.default = function (x, ...) {
|
||||||
#' @describeIn osem_measurements_archive Get daywise measurements for one or
|
#' @describeIn osem_measurements_archive Get daywise measurements for one or
|
||||||
#' more sensors of a single box
|
#' more sensors of a single box
|
||||||
#' @export
|
#' @export
|
||||||
#' @examples
|
#' @examples
|
||||||
#' # fetch measurements for a single day
|
#' # fetch measurements for a single day
|
||||||
#' box = osem_box('593bcd656ccf3b0011791f5a')
|
#' box = osem_box('593bcd656ccf3b0011791f5a')
|
||||||
#' m = osem_measurements_archive(box, as.POSIXlt('2018-09-13'))
|
#' m = osem_measurements_archive(box, as.POSIXlt('2018-09-13'))
|
||||||
#'
|
#'
|
||||||
#' \donttest{
|
#' \donttest{
|
||||||
#' # fetch measurements for a date range and selected sensors
|
#' # fetch measurements for a date range and selected sensors
|
||||||
#' sensors = ~ phenomenon %in% c('Temperatur', 'Beleuchtungsstärke')
|
#' sensors = ~ phenomenon %in% c('Temperatur', 'Beleuchtungsstärke')
|
||||||
|
@ -60,11 +60,11 @@ osem_measurements_archive.default = function (x, ...) {
|
||||||
osem_measurements_archive.sensebox = function (x, fromDate, toDate = fromDate, sensorFilter = ~ T, progress = T) {
|
osem_measurements_archive.sensebox = function (x, fromDate, toDate = fromDate, sensorFilter = ~ T, progress = T) {
|
||||||
if (nrow(x) != 1)
|
if (nrow(x) != 1)
|
||||||
stop('this function only works for exactly one senseBox!')
|
stop('this function only works for exactly one senseBox!')
|
||||||
|
|
||||||
# filter sensors using NSE, for example: `~ phenomenon == 'Temperatur'`
|
# filter sensors using NSE, for example: `~ phenomenon == 'Temperatur'`
|
||||||
sensors = x$sensors[[1]] %>%
|
sensors = x$sensors[[1]] %>%
|
||||||
dplyr::filter(lazyeval::f_eval(sensorFilter, .))
|
dplyr::filter(lazyeval::f_eval(sensorFilter, .))
|
||||||
|
|
||||||
# fetch each sensor separately
|
# fetch each sensor separately
|
||||||
dfs = by(sensors, 1:nrow(sensors), function (sensor) {
|
dfs = by(sensors, 1:nrow(sensors), function (sensor) {
|
||||||
df = archive_fetch_measurements(x, sensor$id, fromDate, toDate, progress) %>%
|
df = archive_fetch_measurements(x, sensor$id, fromDate, toDate, progress) %>%
|
||||||
|
@ -72,7 +72,7 @@ osem_measurements_archive.sensebox = function (x, fromDate, toDate = fromDate, s
|
||||||
#dplyr::mutate(unit = sensor$unit, sensor = sensor$sensor) %>% # inject sensor metadata
|
#dplyr::mutate(unit = sensor$unit, sensor = sensor$sensor) %>% # inject sensor metadata
|
||||||
dplyr::rename_at(., 'value', function(v) sensor$phenomenon)
|
dplyr::rename_at(., 'value', function(v) sensor$phenomenon)
|
||||||
})
|
})
|
||||||
|
|
||||||
# merge all data.frames by timestamp
|
# merge all data.frames by timestamp
|
||||||
dfs %>% purrr::reduce(dplyr::full_join, 'createdAt')
|
dfs %>% purrr::reduce(dplyr::full_join, 'createdAt')
|
||||||
}
|
}
|
||||||
|
@ -85,25 +85,25 @@ archive_fetch_measurements = function (box, sensor, fromDate, toDate, progress)
|
||||||
dates = append(dates, list(from))
|
dates = append(dates, list(from))
|
||||||
from = from + as.difftime(1, units = 'days')
|
from = from + as.difftime(1, units = 'days')
|
||||||
}
|
}
|
||||||
|
|
||||||
http_handle = httr::handle(osem_archive_endpoint()) # reuse the http connection for speed!
|
http_handle = httr::handle(osem_archive_endpoint()) # reuse the http connection for speed!
|
||||||
progress = if (progress && !is_non_interactive()) httr::progress() else NULL
|
progress = if (progress && !is_non_interactive()) httr::progress() else NULL
|
||||||
|
|
||||||
measurements = lapply(dates, function(date) {
|
measurements = lapply(dates, function(date) {
|
||||||
url = build_archive_url(date, box, sensor)
|
url = build_archive_url(date, box, sensor)
|
||||||
res = httr::GET(url, progress, handle = http_handle)
|
res = httr::GET(url, progress, handle = http_handle)
|
||||||
|
|
||||||
if (httr::http_error(res)) {
|
if (httr::http_error(res)) {
|
||||||
warning(paste(
|
warning(paste(
|
||||||
httr::status_code(res),
|
httr::status_code(res),
|
||||||
'on day', format.Date(date, '%F'),
|
'on day', format.Date(date, '%F'),
|
||||||
'for sensor', sensor
|
'for sensor', sensor
|
||||||
))
|
))
|
||||||
|
|
||||||
if (httr::status_code(res) == 404)
|
if (httr::status_code(res) == 404)
|
||||||
return(data.frame(createdAt = character(), value = character()))
|
return(data.frame(createdAt = character(), value = character()))
|
||||||
}
|
}
|
||||||
|
|
||||||
measurements = httr::content(res, type = 'text', encoding = 'UTF-8') %>%
|
measurements = httr::content(res, type = 'text', encoding = 'UTF-8') %>%
|
||||||
parse_measurement_csv
|
parse_measurement_csv
|
||||||
})
|
})
|
||||||
|
@ -117,7 +117,7 @@ build_archive_url = function (date, box, sensor) {
|
||||||
sensorId = sensor
|
sensorId = sensor
|
||||||
d = format.Date(date, '%F')
|
d = format.Date(date, '%F')
|
||||||
format = 'csv'
|
format = 'csv'
|
||||||
|
|
||||||
paste(
|
paste(
|
||||||
osem_archive_endpoint(),
|
osem_archive_endpoint(),
|
||||||
d,
|
d,
|
||||||
|
@ -131,5 +131,5 @@ build_archive_url = function (date, box, sensor) {
|
||||||
#' https://github.com/sensebox/osem-archiver/blob/612e14b/helpers.sh#L66
|
#' https://github.com/sensebox/osem-archiver/blob/612e14b/helpers.sh#L66
|
||||||
osem_box_to_archivename = function (box) {
|
osem_box_to_archivename = function (box) {
|
||||||
name = gsub('[^A-Za-z0-9._-]', '_', box$name)
|
name = gsub('[^A-Za-z0-9._-]', '_', box$name)
|
||||||
paste(box$X_id, name, sep='-')
|
paste(box$X_id, name, sep = '-')
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,17 +96,17 @@ register_s3_method <- function(pkg, generic, class, fun = NULL) {
|
||||||
stopifnot(is.character(pkg), length(pkg) == 1)
|
stopifnot(is.character(pkg), length(pkg) == 1)
|
||||||
stopifnot(is.character(generic), length(generic) == 1)
|
stopifnot(is.character(generic), length(generic) == 1)
|
||||||
stopifnot(is.character(class), length(class) == 1)
|
stopifnot(is.character(class), length(class) == 1)
|
||||||
|
|
||||||
if (is.null(fun)) {
|
if (is.null(fun)) {
|
||||||
fun <- get(paste0(generic, ".", class), envir = parent.frame())
|
fun <- get(paste0(generic, ".", class), envir = parent.frame())
|
||||||
} else {
|
} else {
|
||||||
stopifnot(is.function(fun))
|
stopifnot(is.function(fun))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pkg %in% loadedNamespaces()) {
|
if (pkg %in% loadedNamespaces()) {
|
||||||
registerS3method(generic, class, fun, envir = asNamespace(pkg))
|
registerS3method(generic, class, fun, envir = asNamespace(pkg))
|
||||||
}
|
}
|
||||||
|
|
||||||
# Always register hook in case package is later unloaded & reloaded
|
# Always register hook in case package is later unloaded & reloaded
|
||||||
setHook(
|
setHook(
|
||||||
packageEvent(pkg, "onLoad"),
|
packageEvent(pkg, "onLoad"),
|
||||||
|
|
|
@ -20,7 +20,7 @@ test_that('both from and to are required when requesting boxes, error otherwise'
|
||||||
test_that('a list of boxes with phenomenon filter returns only the requested phenomenon', {
|
test_that('a list of boxes with phenomenon filter returns only the requested phenomenon', {
|
||||||
check_api()
|
check_api()
|
||||||
|
|
||||||
boxes = osem_boxes(phenomenon = 'Temperatur', date=Sys.time())
|
boxes = osem_boxes(phenomenon = 'Temperatur', date = Sys.time())
|
||||||
expect_true(all(grep('Temperatur', boxes$phenomena)))
|
expect_true(all(grep('Temperatur', boxes$phenomena)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue