Skip to content

Commit

Permalink
Add collapse option
Browse files Browse the repository at this point in the history
  • Loading branch information
ConorIA committed Apr 27, 2018
1 parent c1bf283 commit 0a38309
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 19 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: senamhiR
Type: Package
Title: A Collection of Functions to Obtain Peruvian Climate Data
Version: 0.5.1
Date: 2017-10-08
Version: 0.5.2
Date: 2018-04-27
Authors@R: c(person(given = c("Conor", "I."), family = "Anderson",
role = c("aut","cre"), email = "[email protected]"),
person(given = c("William", "A."), family = "Gough", role = "ths",
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(collapse_pcd)
export(download_data_sql)
export(map_stations)
export(qc)
Expand All @@ -13,6 +14,7 @@ importFrom(DBI,dbListTables)
importFrom(DBI,dbReadTable)
importFrom(RMySQL,MySQL)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_rows)
importFrom(dplyr,filter)
importFrom(dplyr,mutate)
importFrom(dplyr,rowwise)
Expand Down
4 changes: 2 additions & 2 deletions R/clean_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
colnames <- c("Fecha", "Nivel06 (m)", "Nivel10 (m)", "Nivel14 (m)", "Nivel18 (m)",
"Caudal (m^3/s)")
if (type == "SUT")
colnames <- c("Fecha", "Tmean (C)", "Tmax (C)", "Tmin (C)", "Humidity (%)",
colnames <- c("Fecha", "Tmean (C)", "Tmax (C)", "Tmin (C)", "Humedad (%)",
"Lluvia (mm)", "Presion (mb)", "Velocidad del Viento (m/s)", "Direccion del Viento",
"Nivel Medio (m)")
} else {
Expand Down Expand Up @@ -62,7 +62,7 @@
datain$`Tmean (C)` <- as.numeric(datain$`Tmean (C)`)
datain$`Tmax (C)` <- as.numeric(datain$`Tmax (C)`)
datain$`Tmin (C)` <- as.numeric(datain$`Tmin (C)`)
datain$`Humidity (%)` <- as.numeric(datain$`Humidity (%)`)
datain$`Humedad (%)` <- as.numeric(datain$`Humedad (%)`)
datain$`Lluvia (mm)` <- as.numeric(datain$`Lluvia (mm)`)
datain$`Presion (mb)` <- as.numeric(datain$`Presion (mb)`)
datain$`Direccion del Viento` <- as.character(datain$`Direccion del Viento`)
Expand Down
18 changes: 18 additions & 0 deletions R/collapse_pcd.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#' Collapse Senamhi stations with common variables
#'
#' @param datain a list of individual station tables acquired through the \code{senamhiR} function.
#'
#' @importFrom dplyr bind_rows
#'
#' @return a list of collapsed stations
#' @export
#'

collapse_pcd <- function(datain) {
dataout <- mapply(add_column, .data = datain, StationID = lapply(datain, attr, "StationID"), MoreArgs = list(.before = 1), SIMPLIFY = FALSE, USE.NAMES = FALSE)
name_groups <- sapply(dataout, function(x) {paste(names(x), collapse = ", ")})
name_groups <- sapply(name_groups, function(x, name_groups) {which(name_groups == x)}, unique(name_groups))
dataout <- lapply(unique(name_groups), function(x, dataout, name_groups) {do.call("bind_rows", dataout[name_groups == x])}, dataout, name_groups)
if(length(dataout) == 1) return(dataout[[1]])
dataout
}
2 changes: 1 addition & 1 deletion R/download_data_sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ download_data_sql <- function(station, year) {
stop("There was an error getting that table.")
}

if (missing(year)) {
if (missing(year) || is.null(year)) {
dat <- as_tibble(dbReadTable(conn, sql_table, row.names = NULL))
} else {
start <- min(year)
Expand Down
27 changes: 15 additions & 12 deletions R/senamhiR.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
##'
##' @param station character; the station id number to process. Can also be a vector of station ids, which will be returned as a list.
##' @param year numerical; a vector of years to process. Defaults to the full known range.
##' @param collapse Boolean; whether multiple stations should be collapsed into a single tibble stations of the same type.
##'
##' @author Conor I. Anderson
##'
Expand All @@ -13,9 +14,9 @@
##'
##' @examples
##' \dontrun{senamhiR('000401', 1998:2015)}
##' \dontrun{senamhiR(c('000401', '000152', '000219'))}
##' \dontrun{senamhiR(c('000401', '000152', '000219'), collapse = TRUE)}

senamhiR <- function(station, year) {
senamhiR <- function(station, year, collapse = FALSE) {
if (missing(station)) {
station <- readline(prompt = "Enter station number(s) separated by commas: ")
station <- trimws(unlist(strsplit(station, split = ",")))
Expand All @@ -31,15 +32,17 @@ senamhiR <- function(station, year) {
stop("One or more requested stations invalid.")
}

dataout <- list()
for (stn in station) {
if (missing(year)) year <- 1900:2100
temp <- download_data_sql(stn, year)
dataout <- c(dataout, list(temp))
}
if (length(station) == 1) {
return(dataout[[1]])
} else {
return(dataout)
pull_data <- function(stn, year) {
rtn <- download_data_sql(stn, year)
attributes(rtn) <- append(attributes(rtn), catalogue[catalogue$StationID == stn,])
rownames(rtn) <- NULL
rtn
}

if (missing(year)) year <- NULL
dataout <- lapply(station, pull_data, year)

if (length(station) == 1) return(dataout[[1]])
if (collapse) return(collapse_pcd(dataout))
dataout
}
17 changes: 17 additions & 0 deletions man/collapse_pcd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/senamhiR.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/test-senamhiR.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ test_that("senamhiR can pad with zeroes", {
expect_output(str(out), "13 variables")
})

## test senamhiR can collapse multiple stations
test_that("senamhiR can collapse stations with similar names", {
out <- senamhiR(c(401, 280, "472D23BE"), year = 2001, collapse = TRUE)
expect_that(out, is_a("list"))
expect_equal(lengths(out), c(14,11))
expect_output(str(out), "List of 2")
})

## should fail when no correct station is given
test_that("senamhiR() fails when an incorrect station is requested", {
expect_error(senamhiR("foo"), "One or more requested stations invalid.", fixed=TRUE)
Expand Down

0 comments on commit 0a38309

Please sign in to comment.