diff --git a/DESCRIPTION b/DESCRIPTION index 6b7d8f0..c1485b6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -8,6 +8,7 @@ Imports: dplyr, httr, digest, + lazyeval, readr, purrr, magrittr diff --git a/R/00utils.R b/R/00utils.R index 1ce9673..a59c136 100644 --- a/R/00utils.R +++ b/R/00utils.R @@ -27,6 +27,7 @@ is_non_interactive = function () { #' custom recursive lapply with better handling of NULL values #' from https://stackoverflow.com/a/38950304 +#' @noRd recursive_lapply = function(x, fn) { if (is.list(x)) lapply(x, recursive_lapply, fn) diff --git a/R/archive.R b/R/archive.R index 71a55b8..a9a0898 100644 --- a/R/archive.R +++ b/R/archive.R @@ -23,11 +23,12 @@ osem_archive_endpoint = function () { #' #' @param x A `sensebox data.frame` of a single box, as retrieved via \code{\link{osem_box}}, #' to download measurements for. -#' @param fromDate Start date for measurement download. +#' @param ... see parameters below +#' @param fromDate Start date for measurement download, must be convertable via `as.Date`. #' @param toDate End date for measurement download (inclusive). #' @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}. -#' @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 \code{\link{osem_measurements}} @@ -44,20 +45,26 @@ osem_measurements_archive.default = function (x, ...) { stop(paste('not implemented for class', toString(class(x)))) } -#' @describeIn osem_measurements_archive Get daywise measurements for one or -#' more sensors of a single box + +# ============================================================================== +# +#' @describeIn osem_measurements_archive Get daywise measurements for one or more sensors of a single box. #' @export #' @examples -#' # fetch measurements for a single day -#' box = osem_box('593bcd656ccf3b0011791f5a') -#' m = osem_measurements_archive(box, as.POSIXlt('2018-09-13')) -#' #' \donttest{ +#' # fetch measurements for a single day +#' box = osem_box('593bcd656ccf3b0011791f5a') +#' m = osem_measurements_archive(box, as.POSIXlt('2018-09-13')) +#' #' # fetch measurements for a date range and selected sensors #' sensors = ~ phenomenon %in% c('Temperatur', 'Beleuchtungsstärke') -#' m = osem_measurements_archive(box, as.POSIXlt('2018-09-01'), as.POSIXlt('2018-09-30'), sensorFilter = sensors) +#' m = osem_measurements_archive( +#' box, +#' as.POSIXlt('2018-09-01'), as.POSIXlt('2018-09-30'), +#' sensorFilter = sensors +#' ) #' } -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) stop('this function only works for exactly one senseBox!') @@ -78,7 +85,14 @@ osem_measurements_archive.sensebox = function (x, fromDate, toDate = fromDate, s } #' fetch measurements from archive from a single box, and a single sensor -archive_fetch_measurements = function (box, sensor, fromDate, toDate, progress) { +#' +#' @param box A sensebox data.frame with a single box +#' @param sensorId Character specifying the sensor +#' @param fromDate Start date for measurement download, must be convertable via `as.Date`. +#' @param toDate End date for measurement download (inclusive). +#' @param progress whether to print progress +#' @return A \code{tbl_df} containing observations of all selected sensors for each time stamp. +archive_fetch_measurements = function (box, sensorId, fromDate, toDate, progress) { dates = list() from = fromDate while (from <= toDate) { @@ -90,14 +104,14 @@ archive_fetch_measurements = function (box, sensor, fromDate, toDate, progress) progress = if (progress && !is_non_interactive()) httr::progress() else NULL measurements = lapply(dates, function(date) { - url = build_archive_url(date, box, sensor) + url = build_archive_url(date, box, sensorId) res = httr::GET(url, progress, handle = http_handle) if (httr::http_error(res)) { warning(paste( httr::status_code(res), 'on day', format.Date(date, '%F'), - 'for sensor', sensor + 'for sensor', sensorId )) if (httr::status_code(res) == 404) @@ -113,8 +127,8 @@ archive_fetch_measurements = function (box, sensor, fromDate, toDate, progress) #' returns URL to fetch measurements from a sensor for a specific date, #' based on `osem_archive_endpoint()` -build_archive_url = function (date, box, sensor) { - sensorId = sensor +#' @noRd +build_archive_url = function (date, box, sensorId) { d = format.Date(date, '%F') format = 'csv' @@ -129,6 +143,9 @@ build_archive_url = function (date, box, sensor) { #' replace chars in box name according to archive script: #' https://github.com/sensebox/osem-archiver/blob/612e14b/helpers.sh#L66 +#' +#' @param box A sensebox data.frame +#' @return character with archive identifier for each box osem_box_to_archivename = function (box) { name = gsub('[^A-Za-z0-9._-]', '_', box$name) paste(box$X_id, name, sep = '-') diff --git a/R/box.R b/R/box.R index 7a5031f..290f6ef 100644 --- a/R/box.R +++ b/R/box.R @@ -165,7 +165,7 @@ parse_senseboxdata = function (boxdata) { # extract metadata from sensors thebox$phenomena = sensors %>% - setNames(lapply(., function (s) s$`_id`)) %>% + stats::setNames(lapply(., function (s) s$`_id`)) %>% lapply(function(s) s$title) %>% unlist %>% list # convert to vector diff --git a/R/opensensmapr.R b/R/opensensmapr.R index 6e6b8a5..4a7bd9b 100644 --- a/R/opensensmapr.R +++ b/R/opensensmapr.R @@ -100,4 +100,13 @@ `%>%` = magrittr::`%>%` # just to make R CMD check happy, due to NSE (dplyr) functions -globalVariables(c('lastMeasurement', '.')) +globalVariables(c( + 'createdAt', + 'lastMeasurement', + 'sensorType', + 'title', + 'unit', + 'value', + 'X_id', + '.' +)) diff --git a/man/archive_fetch_measurements.Rd b/man/archive_fetch_measurements.Rd index 6754ef4..7e95879 100644 --- a/man/archive_fetch_measurements.Rd +++ b/man/archive_fetch_measurements.Rd @@ -4,7 +4,21 @@ \alias{archive_fetch_measurements} \title{fetch measurements from archive from a single box, and a single sensor} \usage{ -archive_fetch_measurements(box, sensor, fromDate, toDate, progress) +archive_fetch_measurements(box, sensorId, fromDate, toDate, progress) +} +\arguments{ +\item{box}{A sensebox data.frame with a single box} + +\item{sensorId}{Character specifying the sensor} + +\item{fromDate}{Start date for measurement download, must be convertable via `as.Date`.} + +\item{toDate}{End date for measurement download (inclusive).} + +\item{progress}{whether to print progress} +} +\value{ +A \code{tbl_df} containing observations of all selected sensors for each time stamp. } \description{ fetch measurements from archive from a single box, and a single sensor diff --git a/man/build_archive_url.Rd b/man/build_archive_url.Rd deleted file mode 100644 index 3508ba6..0000000 --- a/man/build_archive_url.Rd +++ /dev/null @@ -1,13 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/archive.R -\name{build_archive_url} -\alias{build_archive_url} -\title{returns URL to fetch measurements from a sensor for a specific date, -based on `osem_archive_endpoint()`} -\usage{ -build_archive_url(date, box, sensor) -} -\description{ -returns URL to fetch measurements from a sensor for a specific date, -based on `osem_archive_endpoint()` -} diff --git a/man/osem_box_to_archivename.Rd b/man/osem_box_to_archivename.Rd index 8d2707d..5bebcd9 100644 --- a/man/osem_box_to_archivename.Rd +++ b/man/osem_box_to_archivename.Rd @@ -7,6 +7,12 @@ https://github.com/sensebox/osem-archiver/blob/612e14b/helpers.sh#L66} \usage{ osem_box_to_archivename(box) } +\arguments{ +\item{box}{A sensebox data.frame} +} +\value{ +character with archive identifier for each box +} \description{ replace chars in box name according to archive script: https://github.com/sensebox/osem-archiver/blob/612e14b/helpers.sh#L66 diff --git a/man/osem_measurements_archive.Rd b/man/osem_measurements_archive.Rd index a9fdcac..1b21d5b 100644 --- a/man/osem_measurements_archive.Rd +++ b/man/osem_measurements_archive.Rd @@ -8,13 +8,15 @@ osem_measurements_archive(x, ...) \method{osem_measurements_archive}{sensebox}(x, fromDate, - toDate = fromDate, sensorFilter = ~T, progress = T) + toDate = fromDate, sensorFilter = ~T, ..., progress = T) } \arguments{ \item{x}{A `sensebox data.frame` of a single box, as retrieved via \code{\link{osem_box}}, to download measurements for.} -\item{fromDate}{Start date for measurement download.} +\item{...}{see parameters below} + +\item{fromDate}{Start date for measurement download, must be convertable via `as.Date`.} \item{toDate}{End date for measurement download (inclusive).} @@ -23,7 +25,7 @@ to download measurements for.} \item{progress}{Whether to print download progress information, defaults to \code{TRUE}.} } \value{ -A \code{tbl_df} Containing observations of all selected sensors for each time stamp. +A \code{tbl_df} containing observations of all selected sensors for each time stamp. } \description{ This function is significantly faster than \code{\link{osem_measurements}} for large @@ -40,19 +42,22 @@ but continue the remaining download. } \section{Methods (by class)}{ \itemize{ -\item \code{sensebox}: Get daywise measurements for one or -more sensors of a single box +\item \code{sensebox}: Get daywise measurements for one or more sensors of a single box. }} \examples{ # fetch measurements for a single day box = osem_box('593bcd656ccf3b0011791f5a') m = osem_measurements_archive(box, as.POSIXlt('2018-09-13')) - + \donttest{ # fetch measurements for a date range and selected sensors sensors = ~ phenomenon \%in\% c('Temperatur', 'Beleuchtungsstärke') - m = osem_measurements_archive(box, as.POSIXlt('2018-09-01'), as.POSIXlt('2018-09-30'), sensorFilter = sensors) + m = osem_measurements_archive( + box, + as.POSIXlt('2018-09-01'), as.POSIXlt('2018-09-30'), + sensorFilter = sensors + ) } } \seealso{