Skip to content

Commit

Permalink
Merge pull request #333 from amoeba/issue-332-shiny-att-perf
Browse files Browse the repository at this point in the history
Cache calls to units::valid_udunits_prefixes()
  • Loading branch information
cboettig committed Nov 5, 2021
2 parents 6f5806f + 7b1c1c6 commit 4cbd856
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion R/get_unit_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ format_split_unit <- function(split_unit,
return(f_split_unit)
}

# Cache calls to units::valid_udunits_prefixes()$symbol
valid_udunits_prefixes_cache <- new.env(parent = emptyenv())

get_udunits_prefix_symbols <- function() {
if (is.null(valid_udunits_prefixes_cache$symbol)) {
valid_udunits_prefixes_cache$symbol <-
suppressMessages({
units::valid_udunits_prefixes()$symbol
})
}

valid_udunits_prefixes_cache$symbol
}

get_split_unit <- function(unit, exponents) {
if (is.na(unit)) {
unit <- ""
Expand All @@ -187,7 +201,7 @@ get_split_unit <- function(unit, exponents) {
unit <- gsub(
paste0(
"([^[:alpha:]]|^)(",
paste(suppressMessages(units::valid_udunits_prefixes()$symbol), collapse = "|"),
paste(get_udunits_prefix_symbols(), collapse = "|"),
")( )([[:upper:]])"
),
"\\1\\2\\4",
Expand Down

0 comments on commit 4cbd856

Please sign in to comment.