add function wrapper for default endpoint

pull/17/head
nuest 6 years ago
parent d3d758d554
commit ef3fb7f4bb

@ -20,6 +20,7 @@ export(osem_as_sensebox)
export(osem_box) export(osem_box)
export(osem_boxes) export(osem_boxes)
export(osem_counts) export(osem_counts)
export(osem_endpoint)
export(osem_measurements) export(osem_measurements)
export(osem_phenomena) export(osem_phenomena)
export(st_as_sf.osem_measurements) export(st_as_sf.osem_measurements)

@ -4,12 +4,19 @@
# for CSV responses (get_measurements) the readr package is a hidden dependency # for CSV responses (get_measurements) the readr package is a hidden dependency
# ============================================================================== # ==============================================================================
#' Get the default openSenseMap API endpoint
#' @export
#' @return A character string with the HTTP URL of the openSenseMap API
osem_endpoint = function() {
'https://api.opensensemap.org'
}
get_boxes_ = function (..., endpoint) { get_boxes_ = function (..., endpoint) {
response = osem_request_(endpoint, path = c('boxes'), ...) response = osem_request_(endpoint, path = c('boxes'), ...)
if (length(response) == 0) { if (length(response) == 0) {
warning('no boxes found for this query') warning('no boxes found for this query')
return(response) return(osem_as_sensebox(as.data.frame(response)))
} }
# parse each list element as sensebox & combine them to a single data.frame # parse each list element as sensebox & combine them to a single data.frame
@ -17,7 +24,8 @@ get_boxes_ = function (..., endpoint) {
df = dplyr::bind_rows(boxesList) df = dplyr::bind_rows(boxesList)
df$exposure = df$exposure %>% as.factor() df$exposure = df$exposure %>% as.factor()
df$model = df$model %>% as.factor() df$model = df$model %>% as.factor()
df$grouptag = df$grouptag %>% as.factor() if(!is.null(df$grouptag))
df$grouptag = df$grouptag %>% as.factor()
df df
} }

@ -38,7 +38,7 @@
#' #'
osem_boxes = function (exposure = NA, model = NA, grouptag = NA, osem_boxes = function (exposure = NA, model = NA, grouptag = NA,
date = NA, from = NA, to = NA, phenomenon = NA, date = NA, from = NA, to = NA, phenomenon = NA,
endpoint = 'https://api.opensensemap.org', endpoint = osem_endpoint(),
progress = T) { progress = T) {
# error, if phenomenon, but no time given # error, if phenomenon, but no time given
@ -87,7 +87,7 @@ osem_boxes = function (exposure = NA, model = NA, grouptag = NA,
#' # get a specific box by ID #' # get a specific box by ID
#' b = osem_box('57000b8745fd40c8196ad04c') #' b = osem_box('57000b8745fd40c8196ad04c')
#' #'
osem_box = function (boxId, endpoint = 'https://api.opensensemap.org') { osem_box = function (boxId, endpoint = osem_endpoint()) {
get_box_(boxId, endpoint = endpoint) get_box_(boxId, endpoint = endpoint)
} }

@ -33,7 +33,8 @@ plot.sensebox = function (x, ..., mar = c(2,2,1,1)) {
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) data = as.data.frame(x)
print(data[important_columns], ...) available_columns = important_columns %in% names(data)
print(data[available_columns], ...)
invisible(x) invisible(x)
} }

@ -11,6 +11,6 @@
#' #'
#' @export #' @export
#' @seealso \href{https://docs.opensensemap.org/#api-Misc-getStatistics}{openSenseMap API documentation (web)} #' @seealso \href{https://docs.opensensemap.org/#api-Misc-getStatistics}{openSenseMap API documentation (web)}
osem_counts = function (endpoint = 'https://api.opensensemap.org') { osem_counts = function(endpoint = osem_endpoint()) {
get_stats_(endpoint) get_stats_(endpoint)
} }

@ -59,7 +59,7 @@ osem_measurements.default = function (x, ...) {
osem_measurements.bbox = function (x, phenomenon, exposure = NA, osem_measurements.bbox = function (x, phenomenon, exposure = NA,
from = NA, to = NA, columns = NA, from = NA, to = NA, columns = NA,
..., ...,
endpoint = 'https://api.opensensemap.org', endpoint = osem_endpoint(),
progress = T) { progress = T) {
bbox = x bbox = x
environment() %>% environment() %>%
@ -84,7 +84,7 @@ osem_measurements.bbox = function (x, phenomenon, exposure = NA,
osem_measurements.sensebox = function (x, phenomenon, exposure = NA, osem_measurements.sensebox = function (x, phenomenon, exposure = NA,
from = NA, to = NA, columns = NA, from = NA, to = NA, columns = NA,
..., ...,
endpoint = 'https://api.opensensemap.org', endpoint = osem_endpoint(),
progress = T) { progress = T) {
boxes = x boxes = x
environment() %>% environment() %>%

@ -1,9 +1,13 @@
# opensensmapr # opensensmapr
[![CRAN status](http://www.r-pkg.org/badges/version/opensensmapr)](https://cran.r-project.org/package=opensensmapr) [![Travis build status](https://travis-ci.org/noerw/opensensmapR.svg?branch=master)](https://travis-ci.org/noerw/opensensmapR) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/noerw/opensensmapR?branch=master&svg=true)](https://ci.appveyor.com/project/noerw/opensensmapR) [![Coverage status](https://codecov.io/gh/noerw/opensensmapR/branch/master/graph/badge.svg)](https://codecov.io/github/noerw/opensensmapR?branch=master)
This R package ingests data (environmental measurements, sensor stations) from This R package ingests data (environmental measurements, sensor stations) from
the API of opensensemap.org for analysis in R. the API of opensensemap.org for analysis in R.
The package aims to be compatible with sf and the tidyverse. The package aims to be compatible with sf and the tidyverse.
## Installation ## Installation
Right now, the package is not on CRAN. To install it from GitHub, run: Right now, the package is not on CRAN. To install it from GitHub, run:
```r ```r
@ -12,6 +16,7 @@ devtools::install_github('noerw/opensensmapr')
``` ```
## Usage ## Usage
A verbose usage example is shown in the vignette [`osem-intro`](https://noerw.github.com/opensensmapR/inst/doc/osem-intro.html). A verbose usage example is shown in the vignette [`osem-intro`](https://noerw.github.com/opensensmapR/inst/doc/osem-intro.html).
Each functions documentation can be viewed with `?<function-name>`. An overview Each functions documentation can be viewed with `?<function-name>`. An overview
is given in `?opensensmapr`. is given in `?opensensmapr`.
@ -39,17 +44,23 @@ osem_counts()
Additionally there are some helpers: `summary.sensebox(), plot.sensebox(), st_as_sf.sensebox(), osem_as_sensebox(), [.sensebox(), filter.sensebox(), mutate.sensebox(), ...`. Additionally there are some helpers: `summary.sensebox(), plot.sensebox(), st_as_sf.sensebox(), osem_as_sensebox(), [.sensebox(), filter.sensebox(), mutate.sensebox(), ...`.
## Changelog ## Changelog
This project adheres to semantic versioning, for changes in recent versions
please consult [CHANGES.md](CHANGES.md). This project adheres to semantic versioning, for changes in recent versions please consult [CHANGES.md](CHANGES.md).
## FAQ ## FAQ
- *Whats up with that package name?* idk, the R people seem to [enjoy][1] - *Whats up with that package name?* idk, the R people seem to [enjoy][1]
[dropping][2] [vovels][3] so.. Unfortunately I couldn't fit the naming [dropping][2] [vovels][3] so.. Unfortunately I couldn't fit the naming convention to drop an `y` in there.
convention to drop an `y` in there.
[1]: https://github.com/tidyverse/readr [1]: https://github.com/tidyverse/readr
[2]: https://github.com/tidyverse/dplyr [2]: https://github.com/tidyverse/dplyr
[3]: https://github.com/tidyverse/tidyr [3]: https://github.com/tidyverse/tidyr
## Development
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md).
By participating in this project you agree to abide by its terms.
## License ## License
GPL-2.0 - Norwin Roosen GPL-2.0 - Norwin Roosen

@ -4,7 +4,7 @@
\alias{osem_box} \alias{osem_box}
\title{Get a single senseBox by its ID} \title{Get a single senseBox by its ID}
\usage{ \usage{
osem_box(boxId, endpoint = "https://api.opensensemap.org") osem_box(boxId, endpoint = osem_endpoint())
} }
\arguments{ \arguments{
\item{boxId}{A string containing a senseBox ID} \item{boxId}{A string containing a senseBox ID}

@ -5,8 +5,8 @@
\title{Get a set of senseBoxes from the openSenseMap} \title{Get a set of senseBoxes from the openSenseMap}
\usage{ \usage{
osem_boxes(exposure = NA, model = NA, grouptag = NA, date = NA, osem_boxes(exposure = NA, model = NA, grouptag = NA, date = NA,
from = NA, to = NA, phenomenon = NA, from = NA, to = NA, phenomenon = NA, endpoint = osem_endpoint(),
endpoint = "https://api.opensensemap.org", progress = T) progress = T)
} }
\arguments{ \arguments{
\item{exposure}{Only return boxes with the given exposure ('indoor', 'outdoor', 'mobile')} \item{exposure}{Only return boxes with the given exposure ('indoor', 'outdoor', 'mobile')}

@ -4,7 +4,7 @@
\alias{osem_counts} \alias{osem_counts}
\title{Get count statistics of the openSenseMap Instance} \title{Get count statistics of the openSenseMap Instance}
\usage{ \usage{
osem_counts(endpoint = "https://api.opensensemap.org") osem_counts(endpoint = osem_endpoint())
} }
\arguments{ \arguments{
\item{endpoint}{The URL of the openSenseMap API} \item{endpoint}{The URL of the openSenseMap API}

@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/api.R
\name{osem_endpoint}
\alias{osem_endpoint}
\title{Get the default openSenseMap API endpoint}
\usage{
osem_endpoint()
}
\value{
A character string with the HTTP URL of the openSenseMap API
}
\description{
Get the default openSenseMap API endpoint
}

@ -12,12 +12,11 @@ osem_measurements(x, ...)
\method{osem_measurements}{default}(x, ...) \method{osem_measurements}{default}(x, ...)
\method{osem_measurements}{bbox}(x, phenomenon, exposure = NA, from = NA, \method{osem_measurements}{bbox}(x, phenomenon, exposure = NA, from = NA,
to = NA, columns = NA, ..., endpoint = "https://api.opensensemap.org", to = NA, columns = NA, ..., endpoint = osem_endpoint(), progress = T)
progress = T)
\method{osem_measurements}{sensebox}(x, phenomenon, exposure = NA, \method{osem_measurements}{sensebox}(x, phenomenon, exposure = NA,
from = NA, to = NA, columns = NA, ..., from = NA, to = NA, columns = NA, ..., endpoint = osem_endpoint(),
endpoint = "https://api.opensensemap.org", progress = T) progress = T)
} }
\arguments{ \arguments{
\item{x}{Depending on the method, either \item{x}{Depending on the method, either

@ -52,10 +52,10 @@ test_that("endpoint can be (mis)configured", {
expect_error(osem_boxes(endpoint = "http://not.the.opensensemap.org"), "resolve host") expect_error(osem_boxes(endpoint = "http://not.the.opensensemap.org"), "resolve host")
}) })
test_that("a response with no matches returns empty sensebox data.frame", { test_that("a response with no matches returns empty sensebox data.frame and a warning", {
check_api() check_api()
boxes <- osem_boxes(grouptag = "does_not_exist") suppressWarnings(boxes <- osem_boxes(grouptag = "does_not_exist"))
expect_true(is.data.frame(boxes)) expect_true(is.data.frame(boxes))
expect_true(any("sensebox" %in% class(boxes))) expect_true(any("sensebox" %in% class(boxes)))
}) })

@ -0,0 +1,17 @@
context("counts")
check_api <- function() {
code <- NA
try(code <- httr::status_code(httr::GET(osem_endpoint())))
if (is.na(code)) skip("API not available")
}
test_that("counts can be retrieved as a list of numbers", {
check_api()
counts <- osem_counts()
expect_true(is.list(counts))
expect_true(is.numeric(unlist(counts)))
expect_length(counts, 3)
})
Loading…
Cancel
Save