Skip to content

Commit f8f7ed2

Browse files
committed
format_num(): added na_label arg
1 parent dd224d2 commit f8f7ed2

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apexcharter 0.4.5
22
==================
33

44
* Updated ApexCharts.js to 4.7.0 (see https://github.com/apexcharts/apexcharts.js/releases).
5+
* `format_num()`: added `na_label` argument to specify how to represent missing values.
56

67

78

R/format.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,32 @@
77
#' @param suffix Character string to append after formatted value.
88
#' @param locale Localization to use, for example \code{"fr-FR"} for french,
99
#' 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`.
1011
#'
1112
#' @return a \code{JS} function
1213
#' @export
1314
#'
1415
#' @importFrom htmlwidgets JS
1516
#'
1617
#' @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 = "-") {
1823
check_locale_d3(locale)
1924
path <- system.file(file.path("d3-format-locale", paste0(locale, ".json")), package = "apexcharter")
20-
if (path != "") {
25+
if (!identical(path, "")) {
2126
locale <- paste(readLines(con = path, encoding = "UTF-8"), collapse = "")
2227
}
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+
}
2333
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
2636
))
2737
}
2838

man/format_num.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)