Commit c9deeaf9 by Jan Wijffels

Start on constructing appdate + use S4

parent 8f7631c5
......@@ -9,5 +9,5 @@ Authors@R: c(
Description: Utilities for the Citizen Air application.
License: CC-BY-SA-4.0
RoxygenNote: 6.0.1
Imports: readxl, cellranger, data.table, fasttime, rmarkdown, tools, utils
Imports: readxl, cellranger, data.table, fasttime, rmarkdown, tools, utils, sensorweb4R, httr, methods
Suggests: sp
# Generated by roxygen2: do not edit by hand
S3method(as.data.frame,citizenair)
S3method(as.data.frame,citizenair_userdata)
export(read_citizenair)
export(read_stations)
export(run_app)
exportClasses(CitizenAir)
import(methods)
importFrom(cellranger,cell_cols)
importFrom(cellranger,cell_limits)
importFrom(cellranger,cell_rows)
importFrom(data.table,as.data.table)
importFrom(data.table,data.table)
importFrom(data.table,melt.data.table)
importFrom(data.table,rbindlist)
importFrom(data.table,setnames)
importFrom(fasttime,fastPOSIXct)
importFrom(httr,GET)
importFrom(httr,content)
importFrom(readxl,excel_sheets)
importFrom(readxl,read_excel)
importFrom(rmarkdown,run)
importFrom(sensorweb4R,as.Endpoint)
importFrom(sensorweb4R,id)
importFrom(sensorweb4R,label)
importFrom(sensorweb4R,resourceURL)
importFrom(tools,file_path_sans_ext)
importFrom(utils,capture.output)
importFrom(utils,head)
......
setClass(Class="citizenair_stations",
representation=representation(phenomena = "data.frame",
stations = "data.frame"))
setClass(Class="citizenair_userdata",
representation=representation(filename = "character",
file = "character",
time = "POSIXct",
content = "list",
phenomena = "character"))
#' @title Object of class CitizenAir
#' @description Object of class CitizenAir
#' @slot stations Object of class \code{citizenair_stations} as returned by \code{\link{read_stations}}
#' @slot citizen_data Object of class \code{citizenair_userdata} as returned by \code{\link{read_citizenair}}
#' @name CitizenAir-class
#' @rdname CitizenAir-class
#' @param endpoint An object of class Endpoint as returned by \code{as.Endpoint}
#' @export
#' @examples
#' library(sensorweb4R)
#' e <- as.Endpoint("http://geo.irceline.be/sos/api/v1")
#' ca <- new("CitizenAir", endpoint=e)
#' ca
setClass(Class = "CitizenAir",
representation = representation(stations = "citizenair_stations",
citizen_data = "citizenair_userdata"))
setMethod(f = "initialize",
signature = "CitizenAir",
definition = function(.Object, endpoint) {
.Object@stations <- read_stations(endpoint)
.Object@citizen_data <- citizenair_userdata_empty()
.Object
})
......@@ -2,9 +2,12 @@
#' @importFrom cellranger cell_rows cell_cols cell_limits
#' @importFrom utils head tail capture.output
#' @importFrom tools file_path_sans_ext
#' @importFrom data.table rbindlist melt.data.table setnames as.data.table
#' @importFrom data.table rbindlist melt.data.table setnames as.data.table data.table
#' @importFrom fasttime fastPOSIXct
#' @importFrom rmarkdown run
#' @importFrom sensorweb4R resourceURL id label as.Endpoint
#' @importFrom httr GET content
#' @import methods
NULL
......@@ -6,7 +6,7 @@
#' @param name the name of the file, defaults to the basename of the file
#' @export
#' @return
#' an object of class \code{citizenair} which is a list with elements
#' an object of class \code{citizenair_userdata} which is a list with elements
#' \enumerate{
#' \item{filename: the name of the file}
#' \item{file: the full path to the excel file}
......@@ -30,7 +30,7 @@
#' \item{several columns, one for each phenomena indicated in the \code{meta} element}
#' }
#' @examples
#' filename <- system.file(package = "citizenair", "data-raw", "test_dummy.xls")
#' filename <- system.file(package = "citizenair", "data-raw", "citizenair-example.xls")
#' x <- read_citizenair(filename)
#' meta <- as.data.frame(x, type = "meta")
#' measurements <- as.data.frame(x, type = "measurements")
......@@ -53,13 +53,24 @@ read_citizenair <- function(file = system.file(package = "citizenair", "data-raw
result$content[[sheet]]$data <- read_sheet_measurements(file, sheet, sheet_details)
}
result$phenomena <- unique(sort(unlist(lapply(result$content, FUN=function(x) names(x$meta$phenomena)))))
class(result) <- "citizenair"
class(result) <- "citizenair_userdata"
result
}
citizenair_userdata_empty <- function(){
result <- list()
result$filename <- NA_character_
result$file <- NA_character_
result$time <- Sys.time()
result$content <- list()
result$phenomena <- character()
class(result) <- "citizenair_userdata"
result
}
#' @title Convert excel data to a data.frame
#' @description Convert excel data to a data.frame
#' @param x an object of class \code{citizenair} as returned by \code{\link{read_citizenair}}
#' @param x an object of class \code{citizenair_userdata} as returned by \code{\link{read_citizenair}}
#' @param type either 'meta' to get the meta information as a data.frame or 'measurements' to get the measurements data.frame
#' @param row.names not used
#' @param optional not used
......@@ -71,11 +82,11 @@ read_citizenair <- function(file = system.file(package = "citizenair", "data-raw
#' \item{in case type is data: contains fields sheet_id, timepoint, date, phenomena, value}
#' }
#' @examples
#' filename <- system.file(package = "citizenair", "data-raw", "test_dummy.xls")
#' filename <- system.file(package = "citizenair", "data-raw", "citizenair-example.xls")
#' x <- read_citizenair(filename)
#' meta <- as.data.frame(x, type = "meta")
#' measurements <- as.data.frame(x, type = "measurements")
as.data.frame.citizenair <- function(x, row.names, optional, ..., type = c("meta", "measurements")){
as.data.frame.citizenair_userdata <- function(x, row.names, optional, ..., type = c("meta", "measurements")){
type <- match.arg(type)
if(type == "meta"){
x <- lapply(x$content, FUN=function(x){
......@@ -153,7 +164,7 @@ read_sheet_info <- function(file, sheet){
measurements_columns = measurements_columns,
measurements_fields = measurements_fields,
data_n = length(datums))
class(result) <- "citizenair_excel_locations"
class(result) <- "citizenair_userdata_excel_locations"
result
}
......
#' @title Get CitizenAir Stations
#' @description Get CitizenAir Stations
#' @param x an object of class \code{Endpoint} as returned by \code{as.Endpoint}
#' @export
#' @return
#' an object of class \code{citizenair_stations} which is a list with elements \code{phenomena} and \code{stations}\cr
#' Element phenomena is a data.table with all phenomena containing fields
#' \enumerate{
#' \item{id: identifier of the phenomena}
#' \item{label: label of the phenomena}
#' }
#' Element stations is a data.table with all stations containing fields
#' \enumerate{
#' \item{id: identifier of the station}
#' \item{label: label of the station}
#' \item{lon: longitude of the station}
#' \item{lat: latitude of the station}
#' \item{phenomena: a list of phenomena identifiers for each station}
#' \item{timeseries: a list of timeseries identifiers for each station}
#' }
#' @examples
#' library(sensorweb4R)
#' e <- as.Endpoint("http://geo.irceline.be/sos/api/v1")
#' x <- read_stations(e)
read_stations <- function(x = sensorweb4R::as.Endpoint("http://geo.irceline.be/sos/api/v1")){
stopifnot(inherits(x, "Endpoint"))
e <- x
url <- sensorweb4R::resourceURL(e)
phe <- data.table(id = sensorweb4R::id(sensorweb4R::phenomena(e)),
label = sensorweb4R::label(sensorweb4R::phenomena(e)),
stringsAsFactors = FALSE)
phe <- phe[order(phe$label, decreasing = FALSE), ]
sta <- httr::GET(url = sprintf("%s/%s", url, "stations"), query = list(expanded="true"))
sta <- httr::content(sta)
sta <- lapply(sta, FUN=function(x){
out <- list(id = x$properties$id,
label = x$properties$label,
lon = x$geometry$coordinates[[1]],
lat = x$geometry$coordinates[[2]],
phenomena = sort(unique(sapply(x$properties$timeseries, FUN=function(x) x$phenomenon$id))),
timeseries = sort(unique(names(x$properties$timeseries))))
out
})
stat <- rbindlist(lapply(sta, FUN=function(x) x[c("id", "label", "lon", "lat")]))
stat$phenomena <- lapply(sta, FUN=function(x) x$phenomena)
stat$timeseries <- lapply(sta, FUN=function(x) x$timeseries)
result <- list()
result$phenomena <- phe
result$stations <- stat
class(result) <- "citizenair_stations"
result
}
\ No newline at end of file
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/appdata.R
\docType{class}
\name{CitizenAir-class}
\alias{CitizenAir-class}
\title{Object of class CitizenAir}
\arguments{
\item{endpoint}{An object of class Endpoint as returned by \code{as.Endpoint}}
}
\description{
Object of class CitizenAir
}
\section{Slots}{
\describe{
\item{\code{stations}}{Object of class \code{citizenair_stations} as returned by \code{\link{read_stations}}}
\item{\code{citizen_data}}{Object of class \code{citizenair_userdata} as returned by \code{\link{read_citizenair}}}
}}
\examples{
library(sensorweb4R)
e <- as.Endpoint("http://geo.irceline.be/sos/api/v1")
ca <- new("CitizenAir", endpoint=e)
ca
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/read-citizendata.R
\name{as.data.frame.citizenair}
\alias{as.data.frame.citizenair}
\name{as.data.frame.citizenair_userdata}
\alias{as.data.frame.citizenair_userdata}
\title{Convert excel data to a data.frame}
\usage{
\method{as.data.frame}{citizenair}(x, row.names, optional, ...,
\method{as.data.frame}{citizenair_userdata}(x, row.names, optional, ...,
type = c("meta", "measurements"))
}
\arguments{
\item{x}{an object of class \code{citizenair} as returned by \code{\link{read_citizenair}}}
\item{x}{an object of class \code{citizenair_userdata} as returned by \code{\link{read_citizenair}}}
\item{row.names}{not used}
......@@ -29,7 +29,7 @@ a data.frame is returned
Convert excel data to a data.frame
}
\examples{
filename <- system.file(package = "citizenair", "data-raw", "test_dummy.xls")
filename <- system.file(package = "citizenair", "data-raw", "citizenair-example.xls")
x <- read_citizenair(filename)
meta <- as.data.frame(x, type = "meta")
measurements <- as.data.frame(x, type = "measurements")
......
......@@ -13,7 +13,7 @@ read_citizenair(file = system.file(package = "citizenair", "data-raw",
\item{name}{the name of the file, defaults to the basename of the file}
}
\value{
an object of class \code{citizenair} which is a list with elements
an object of class \code{citizenair_userdata} which is a list with elements
\enumerate{
\item{filename: the name of the file}
\item{file: the full path to the excel file}
......@@ -41,7 +41,7 @@ Content element data is a data.frame with the measurements containing
Read in a CitizenAir excel file
}
\examples{
filename <- system.file(package = "citizenair", "data-raw", "test_dummy.xls")
filename <- system.file(package = "citizenair", "data-raw", "citizenair-example.xls")
x <- read_citizenair(filename)
meta <- as.data.frame(x, type = "meta")
measurements <- as.data.frame(x, type = "measurements")
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/read-stations.R
\name{read_stations}
\alias{read_stations}
\title{Get CitizenAir Stations}
\usage{
read_stations(x = sensorweb4R::as.Endpoint("http://geo.irceline.be/sos/api/v1"))
}
\arguments{
\item{x}{an object of class \code{Endpoint} as returned by \code{as.Endpoint}}
}
\value{
an object of class \code{citizenair_stations} which is a list with elements \code{phenomena} and \code{stations}\cr
Element phenomena is a data.table with all phenomena containing fields
\enumerate{
\item{id: identifier of the phenomena}
\item{label: label of the phenomena}
}
Element stations is a data.table with all stations containing fields
\enumerate{
\item{id: identifier of the station}
\item{label: label of the station}
\item{lon: longitude of the station}
\item{lat: latitude of the station}
\item{phenomena: a list of phenomena identifiers for each station}
\item{timeseries: a list of timeseries identifiers for each station}
}
}
\description{
Get CitizenAir Stations
}
\examples{
library(sensorweb4R)
e <- as.Endpoint("http://geo.irceline.be/sos/api/v1")
x <- read_stations(e)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment