|
7 | 7 | #' @param suffix Character string to append after formatted value. |
8 | 8 | #' @param locale Localization to use, for example \code{"fr-FR"} for french, |
9 | 9 | #' see possible values here: \url{https://github.com/d3/d3-format/tree/master/locale}. |
| 10 | +#' @param na_label The label to use when value is `NA`. |
10 | 11 | #' |
11 | 12 | #' @return a \code{JS} function |
12 | 13 | #' @export |
13 | 14 | #' |
14 | 15 | #' @importFrom htmlwidgets JS |
15 | 16 | #' |
16 | 17 | #' @example examples/format.R |
17 | | -format_num <- function(format, prefix = "", suffix = "", locale = "en-US") { |
| 18 | +format_num <- function(format, |
| 19 | + prefix = "", |
| 20 | + suffix = "", |
| 21 | + locale = "en-US", |
| 22 | + na_label = "-") { |
18 | 23 | check_locale_d3(locale) |
19 | 24 | path <- system.file(file.path("d3-format-locale", paste0(locale, ".json")), package = "apexcharter") |
20 | | - if (path != "") { |
| 25 | + if (!identical(path, "")) { |
21 | 26 | locale <- paste(readLines(con = path, encoding = "UTF-8"), collapse = "") |
22 | 27 | } |
| 28 | + if (is.character(na_label) && length(na_label) == 1) { |
| 29 | + na_label <- sprintf("if (value === null) return '%s';", na_label) |
| 30 | + } else { |
| 31 | + na_label <- "" |
| 32 | + } |
23 | 33 | JS(sprintf( |
24 | | - "function(value) {var locale = formatLocale(JSON.parse('%s')); return '%s' + locale.format('%s')(value) + '%s';}", |
25 | | - locale, prefix, format, suffix |
| 34 | + "function(value) {%s var locale = formatLocale(JSON.parse('%s')); return '%s' + locale.format('%s')(value) + '%s';}", |
| 35 | + na_label, locale, prefix, format, suffix |
26 | 36 | )) |
27 | 37 | } |
28 | 38 |
|
|
0 commit comments