Skip to contents

[Questioning]

This function was an experimental attempt at inline formatting, but is likely to be removed from epoxy in favor of epoxy_style_inline().

Makes available all of the label_ functions from the scales package within an inline format function, fmt(). To apply formatting, wrap embraced expressions in fmt(expr, label), where label is the name of a labeller function from scales (with or without the leading label_), e.g. "dollar" or "label_dollar" for scales::label_dollar(). Short label forms exist for some labellers, e.g. "$" for "dollar". See the full listing in the table below.

```{r}
# Airbnb stats that may not be completely accurate
airbnb <- list(
 countries = 220,
 cities = 1e5,
 avg_stay = 4.326,
 avg_cost = 184.952,
 hosts = 4.12e6
)
```

With standard inline R code in R Markdown, we can write the following:

* Airbnb includes listings in `r airbnb$cities` cities
  in `r airbnb$countries` countries
  from around `r airbnb$hosts` hosts.
  The average guest stays `r airbnb$avg_stay` nights
  at a rate of `r airbnb$avg_cost` per night.

  • Airbnb includes listings in 10^5 cities in 220 countries from around 4.12 × 10^6 hosts. The average guest stays 4.326 nights at a rate of 184.952 per night.

Using epoxy and the light-weight fmt() function from epoxy_style_format(), we can improve the readability and formatting of the interwoven numbers.

```{r my_style, echo = FALSE}
# Define number/dollar styles to apply consistently
my_style <- epoxy_style_format(
 dollar = scales::label_dollar(accuracy = 1),
 number = scales::label_number(
   accuracy = 0.1,
   scale_cut = scales::cut_short_scale()
 )
)
```

```{epoxy epoxy_style = my_style}
* Airbnb includes listings in {fmt(airbnb$cities, ",")} cities
  in {fmt(airbnb$countries, "auto")} countries
  from around {fmt(airbnb$hosts, "#")} hosts.
  The average guest stays {fmt(airbnb$avg_stay, "#")} nights
  at a rate of {fmt(airbnb$avg_cost, "$")} per night.
```

  • Airbnb includes listings in 100,000 cities in 220 countries from around 4.1M hosts. The average guest stays 4.3 nights at a rate of $185 per night.

fmt() labels for formatters

labelApplies formatting with
"bytes"scales::label_bytes()
"d", "dt", "dttm", "date", "time", "datetime"function()
"d", "dt", "dttm", "date", "time", "datetime"function()
"d", "dt", "dttm", "date", "time", "datetime"function()
"$", "dollar"scales::label_dollar()
"log"scales::label_log()
"#", "number"scales::label_number()
",", "comma"scales::label_comma()
"a", "auto", "number_auto"scales::label_number_auto()
"o", "ordinal"scales::label_ordinal()
"parse"scales::label_parse()
"pct", "%", "percent"scales::label_percent()
"p", "pvalue"scales::label_pvalue()
"si", "scientific"scales::label_scientific()
"wrap"scales::label_wrap()
"uc", "uppercase"toupper()
"lc", "lowercase"tolower()
"tc", "titlecase"tools::toTitleCase()

Usage

epoxy_style_format(
  bytes = scales::label_bytes(),
  date = function(x) format(x, format = "%F"),
  time = function(x) format(x, format = "%T"),
  datetime = function(x) format(x, format = "%F %T"),
  dollar = scales::label_dollar(),
  log = scales::label_log(),
  number = scales::label_number(),
  comma = scales::label_comma(),
  number_auto = scales::label_number_auto(),
  ordinal = scales::label_ordinal(),
  parse = scales::label_parse(),
  percent = scales::label_percent(),
  pvalue = scales::label_pvalue(),
  scientific = scales::label_scientific(),
  wrap = scales::label_wrap(width = 80),
  uppercase = toupper,
  lowercase = tolower,
  titlecase = tools::toTitleCase,
  ...,
  transformer = glue::identity_transformer
)

Arguments

bytes

The function to apply to when label is "bytes". Default is scales::label_bytes().

date

The function to apply to when label is "date" or "d". Default is function().

time

The function to apply to when label is "time" or "dt". Default is function().

datetime

The function to apply to when label is "datetime" or "dttm". Default is function().

dollar

The function to apply to when label is "dollar" or "$". Default is scales::label_dollar().

log

The function to apply to when label is "log". Default is scales::label_log().

number

The function to apply to when label is "number" or "#". Default is scales::label_number().

comma

The function to apply to when label is "comma" or ",". Default is scales::label_comma().

number_auto

The function to apply to when label is "number_auto", "a", or "auto". Default is scales::label_number_auto().

ordinal

The function to apply to when label is "ordinal" or "o". Default is scales::label_ordinal().

parse

The function to apply to when label is "parse". Default is scales::label_parse().

percent

The function to apply to when label is "percent", "pct", or "%". Default is scales::label_percent().

pvalue

The function to apply to when label is "pvalue" or "p". Default is scales::label_pvalue().

scientific

The function to apply to when label is "scientific" or "si". Default is scales::label_scientific().

wrap

The function to apply to when label is "wrap". Default is scales::label_wrap().

uppercase

The function to apply to when label is "uppercase" or "uc". Default is toupper().

lowercase

The function to apply to when label is "lowercase" or "lc". Default is tolower().

titlecase

The function to apply to when label is "titlecase" or "tc". Default is tools::toTitleCase().

...

Additional formatting functions as named arguments. The name of the argument in ... determines the label value associated with the formatter in fmt().

For example, providing url = utils::URLencode would allow you to apply URL-encoding formatting using fmt(expr, "url").

transformer

The transformer to apply to the replacement string. This argument is used for chaining the transformer functions. By providing a function to this argument you can apply an additional transformation after the current transformation. In nearly all cases, you can let epoxy_style() handle this for you. The chain ends when glue::identity_transformer() is used as the transformer.

Value

A function of text and envir suitable for the .transformer

argument of glue::glue().

See also

Other epoxy-style glue transformers: epoxy_style_html(), epoxy_style_inline(), epoxy_style()

Examples

revenue <- 0.2123
sales <- 42000.134
glue::glue(
  '{fmt(revenue, "%")} of revenue generates {fmt(sales, "$")} in profits.',
  .transformer = epoxy_style_format()
)
#> 21% of revenue generates $42,000.13 in profits.

# To set labeller options, provide the label calls
glue::glue(
  '{fmt(revenue, "%")} of revenue generates {fmt(sales, "$")} in profits.',
  .transformer = epoxy_style_format(
    percent = scales::label_percent(accuracy = 0.1),
    dollar = scales::label_dollar(accuracy = 10)
  )
)
#> 21.2% of revenue generates $42,000 in profits.

# Add your own formatting functions
search <- "why are cats scared of cucumbers"
glue::glue(
  '<https://example.com?q={fmt(search, "url")}>',
  .transformer = epoxy_style_format(
    url = utils::URLencode
  )
)
#> <https://example.com?q=why%20are%20cats%20scared%20of%20cucumbers>