use_d3_format: Use d3.format with a htmlwidget

Description Usage Arguments Value Examples

View source: R/widget.R

Description

Format numbers in htmlwidgets with d3.format.

Usage

1
2
3
use_d3_format(widget)

d3_formatter(specifier, prefix = "", suffix = "", locale = "en-US")

Arguments

widget

An object of type htmlwidget.

specifier

Format for numbers, currency, percentage, e.g. ".0%" for rounded percentage. See online documentation : https://github.com/d3/d3-format.

prefix

Character string to append before formatted value.

suffix

Character string to append after formatted value.

locale

Localization to use, for example "fr-FR" for french, see possible values here: https://github.com/d3/d3-format/tree/master/locale.

Value

The same htmlwidget with an extra dependency.

a JS function

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
library(d3.format)

dat <- data.frame(
  x = c("format", "numeric", "values", "with ease"),
  y = c(1233172L, 1467863L, 1953877L, 1382088L)
)


# With highcharter
library(highcharter)
hchart(dat, mapping = hcaes(x, y), type = "column") %>%
  use_d3_format() %>%
  hc_yAxis(labels = list(
    formatter = d3_formatter(",.3~s", suffix = " unit")
  ))

# with apexcharter
library(apexcharter)
apex(dat, aes(x, y), "column") %>%
  use_d3_format() %>%
  ax_yaxis(labels = list(
    formatter =  d3_formatter(",.3~s", suffix = " unit")
  ))


# With billboarder
library(billboarder)
billboarder(data = dat) %>%
  bb_aes(x = x, y = y) %>%
  bb_barchart() %>%
  use_d3_format() %>%
  bb_y_axis(
    tick = list(
      format = d3_formatter(",.3~s", suffix = " unit")
    )
  )

# with echarts4r
library(echarts4r)
dat %>%
  e_charts(x) %>%
  use_d3_format() %>%
  e_bar(y, name = "Serie 1") %>%
  e_y_axis(
    formatter = d3_formatter(",.3~s", suffix = " unit"),
    offset = -10
  )

dreamRs/d3.format documentation built on June 7, 2020, 5:15 a.m.