Skip to content

Commit

Permalink
Cache calls to units::valid_udunits_prefixes()
Browse files Browse the repository at this point in the history
This fixes a performance issue with the entire shiny_attributes app reported in ropensci#332.
  • Loading branch information
amoeba committed Nov 4, 2021
1 parent 6f5806f commit 7b1c1c6
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 7b1c1c6

Please sign in to comment.