Skip to content

Commit

Permalink
Merge pull request #354 from davidcarslaw/fix/programming-refinements
Browse files Browse the repository at this point in the history
Prefer `is.null()` (and `is.na()`) over `missing()`
  • Loading branch information
davidcarslaw authored Aug 4, 2023
2 parents 2e23c72 + 021d349 commit 2fa97c8
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 247 deletions.
8 changes: 5 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

- `source = "all"` will return all available metadata (including KCL and Europe).

- new function `runRegression()` for extracting 'dilution lines' from air quality and other data. Online manual will be updated with principles and examples.

- `calendarPlot()` now automatically creates its own `labels` if `breaks` are specified. For example, `c(0, 10, 20)` will create the labels `c("0 - 10", "10 - 20")`. `labels` can still be used to override the default values. (#341)

- return tibble from `timeAverage()`.
Expand All @@ -32,9 +34,7 @@

- Move regression formula off main plot for `polarPlot()` for clarity and label slope as 'm'.

- new function `runRegression()` for extracting 'dilution lines' from air quality and other data. Online manual will be updated with principles and examples.

- Tweak seasonal trend decomposition using STL to allow the seasonal amplitude to vary more. Affects `smoothTrend` and `TheilSen`.
- Tweak seasonal trend decomposition using STL to allow the seasonal amplitude to vary more. Affects `smoothTrend()` and `TheilSen()`.

## Bug Fixes

Expand All @@ -44,6 +44,8 @@

- The `year` argument of `importMeta()` is now respected when `source = "kcl"` and `"europe"`.

- Several of the directional analysis plot family (e.g., `polarFreq()`) have been refactored to use `is.null()` or `is.na()` over `missing()`. While predominantly an internal change, this should be make these functions easier to use inside of other functions (e.g., `function(data, breaks = NA) polarFreq(data, breaks = breaks))` will now run successfully).

- For `calendarPlot` when annotated with ws or wd arrows, use max ws/wd that corresponds to hour of maximum pollutant concentration and not simple the max ws/wd for a day.

# openair 2.17-0
Expand Down
4 changes: 1 addition & 3 deletions R/percentileRose.R
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,7 @@ percentileRose <- function(

## nice intervals for pollutant concentrations
tmp <- (results.grid$x ^ 2 + results.grid$y ^ 2) ^ 0.5
if (missing(intervals)) intervals <- pretty(c(min(tmp, na.rm = TRUE), max(tmp, na.rm = TRUE)))


if (is.null(intervals)) intervals <- pretty(c(min(tmp, na.rm = TRUE), max(tmp, na.rm = TRUE)))

labs <- intervals ## the labels

Expand Down
7 changes: 3 additions & 4 deletions R/polarAnnulus.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ polarAnnulus <-
type = "default",
statistic = "mean",
percentile = NA,
limits = c(0, 100),
limits = NULL,
cols = "default",
width = "normal",
min.bin = 1,
Expand All @@ -159,7 +159,7 @@ polarAnnulus <-
force.positive = TRUE,
k = c(20, 10),
normalise = FALSE,
key.header = "",
key.header = statistic,
key.footer = pollutant,
key.position = "right",
key = TRUE,
Expand All @@ -183,7 +183,6 @@ polarAnnulus <-
percentile <- 50
}

if (missing(key.header)) key.header <- statistic
if (key.header[1] == "weighted.mean") key.header <- c("weighted", "mean")
if (key.header[1] == "percentile") key.header <- c(paste(percentile, "th", sep = ""), "percentile")
if (key.header[1] == "cpf") key.header <- c("CPF", "probability")
Expand Down Expand Up @@ -510,7 +509,7 @@ polarAnnulus <-
## auto-scaling
nlev <- 200 # preferred number of intervals
## handle missing breaks arguments
if (missing(limits)) {
if (any(is.null(limits)) | any(is.na(limits))) {
# breaks <- pretty(results.grid$z, n = nlev)
breaks <- seq(min(results.grid$z, na.rm = TRUE), max(results.grid$z, na.rm = TRUE),
length.out = nlev
Expand Down
5 changes: 4 additions & 1 deletion R/polarDiff.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ polarDiff <- function(
after,
pollutant = "nox",
x = "ws",
limits = NA,
limits = NULL,
plot = TRUE,
...
) {
if (is.null(limits)){
limits <- NA
}

# extra args setup
Args <- list(...)
Expand Down
21 changes: 11 additions & 10 deletions R/polarFreq.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@
#' \dontrun{polarFreq(mydata, pollutant = "pm25", statistic
#' ="weighted.mean", offset = 50, ws.int = 25, trans = FALSE) }
polarFreq <- function(mydata,
pollutant = "",
pollutant = NULL,
statistic = "frequency",
ws.int = 1,
wd.nint = 36,
grid.line = 5,
breaks = seq(0, 5000, 500),
breaks = NULL,
cols = "default",
trans = TRUE,
type = "default",
Expand Down Expand Up @@ -183,7 +183,7 @@ polarFreq <- function(mydata,
trellis.par.set(fontsize = list(text = extra.args$fontsize))
}

if (!missing(pollutant)) vars <- c(vars, pollutant)
if (!is.null(pollutant)) vars <- c(vars, pollutant)

## data checks
mydata <- checkPrep(mydata, vars, type, remove.calm = FALSE)
Expand All @@ -198,19 +198,20 @@ polarFreq <- function(mydata,
mydata <- cutData(mydata, type, ...)

## if pollutant chosen but no statistic - use mean, issue warning
if (!missing(pollutant) & missing(statistic)) {
if (statistic == "frequency" & !is.null(pollutant)) {
cli::cli_warn(c("x" = "{.code statistic == 'frequency'} incompatible with a defined {.field pollutant}.",
"i" = "Setting {.field statistic} to {.code 'mean'}."))
statistic <- "mean"
warning("No statistic chosen, using mean")
}

## if statistic chosen but no pollutant stop
if (!missing(statistic) & missing(pollutant)) {
stop("No pollutant chosen, please choose one e.g. pollutant = 'nox'")
if (statistic != "frequency" & is.null(pollutant)) {
cli::cli_abort(c("x" = "No {.field pollutant} chosen",
"i" = "Please choose a {.field pollutant}, e.g., {.code pollutant = 'nox'}"))
}

if (!missing(breaks)) trans <- FALSE ## over-ride transform if breaks supplied
if (!(any(is.null(breaks)) | any(is.na(breaks)))) trans <- FALSE ## over-ride transform if breaks supplied

if (missing(key.header)) key.header <- statistic
if (key.header == "weighted.mean") key.header <- c("contribution", "(%)")

## apply square root transform?
Expand Down Expand Up @@ -319,7 +320,7 @@ polarFreq <- function(mydata,

nlev <- 200
## handle missing breaks arguments
if (missing(breaks)) {
if (any(is.null(breaks)) | any(is.na(breaks))) {
breaks <- unique(c(0, pretty(results.grid$weights, nlev)))
br <- pretty((c(0, results.grid$weights) ^ coef), n = 10) ## breaks for scale
} else {
Expand Down
Loading

0 comments on commit 2fa97c8

Please sign in to comment.