Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Closes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
HughParsonage committed Jun 7, 2017
1 parent 79eafae commit fdee7bd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Imports:
httr,
magrittr,
readr,
rjson,
jsonlite,
tidyr,
tools,
utils,
Expand Down
29 changes: 21 additions & 8 deletions R/get_current_weather.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' \code{latlon} is selected, a message is emitted before the table is returned
#' indicating which station was actually used (i.e. which station was found to
#' be nearest to the given coordinate).
#' @param as.data.table Return result as a \code{data.table}.
#' @details Note that the column \code{local_date_time_full} is set to a
#' \code{POSIXct} object in the local time of the \strong{user}.
#' For more details see the vignette "Current Weather Fields":
Expand All @@ -39,7 +40,8 @@ get_current_weather <-
function(station_name,
latlon = NULL,
raw = FALSE,
emit_latlon_msg = TRUE) {
emit_latlon_msg = TRUE,
as.data.table = FALSE) {
if (missing(station_name) && is.null(latlon)) {
stop("One of 'station_name' or 'latlon' must be provided.")
}
Expand Down Expand Up @@ -120,7 +122,7 @@ get_current_weather <-
}

observations.json <-
rjson::fromJSON(file = json_url)
jsonlite::fromJSON(txt = json_url)

if ("observations" %notin% names(observations.json) ||
"data" %notin% names(observations.json$observations)) {
Expand All @@ -136,7 +138,11 @@ get_current_weather <-
"cloud_oktas",
"rain_trace")
# (i.e. not raw)
cook <- function(DT) {
cook <- function(DT, as.DT) {
if (!is.data.table(DT)) {
setDT(DT)
}

DTnoms <- names(DT)

# CRAN NOTE avoidance
Expand All @@ -161,19 +167,26 @@ get_current_weather <-
for (j in which(DTnoms %chin% double_cols)) {
set(DT, j = j, value = force_double(DT[[j]]))
}

if (!as.DT) {
DT <- as.data.frame(DT)
}

DT[]
}

out <-
observations.json %>%
use_series("observations") %>%
use_series("data") %>%
lapply(as.data.table) %>%
rbindlist(use.names = TRUE, fill = TRUE)
use_series("data")

if (as.data.table) {
setDT(out)
}

if (raw) {
return(as.data.frame(out))
return(out)
} else {
return(as.data.frame(cook(out)))
return(cook(out, as.DT = as.data.table))
}
}
4 changes: 3 additions & 1 deletion man/get_current_weather.Rd

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

5 changes: 5 additions & 0 deletions tests/testthat/test-get_current_weather.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ test_that("latlon: Query of c(-27, 149) returns Surat (QLD, between Roma and St
expect_equal(unique(Surat$name), "Surat")
})

test_that("Data table if requested", {
YMML <- get_current_weather("Melbourne Airport", as.data.table = TRUE)
expect_true("data.table" %in% class(YMML))
})

0 comments on commit fdee7bd

Please sign in to comment.