
Make your text mighty fine
{verbaliseR} is a collection of functions that make it easier to turn R analysis outputs into sentences. Here’s a quick example:
library(dplyr)
unique_species <- palmerpenguins::penguins %>%
pull(species) %>%
unique()
paste0(
"There are ",
verbaliseR::pluralise(
word = "species",
count = length(unique_species),
plural = "species",
add_or_swap = "swap"
),
" of penguins in this dataset: ",
verbaliseR::listify(unique_species, linking_word = "and"),
"."
)Installation
Install from GitHub:
# From CRAN
install.packages("verbaliseR")
# From Github...
# ... if you don't already have the {remotes} package installed:
install.packages("remotes")
# ... then:
remotes::install_github("cararthompson/verbaliseR")
pluralise()
Takes a string and turns it into its plural, based on user input. It also retains the number, applying the rules of num_to_text() to it, unless specified otherwise. The flexibility of this function means it can be used in any language, but since numbers are currently returned only in English, users of other languages will need to specify include_number = FALSE for now if the number is between 1 and 10 or if sentence_start is TRUE.
# The default plural is an s tagged onto the end of the word ...
verbaliseR::pluralise("penguin", 3)
# ... but this can be changed ...
verbaliseR::pluralise("bateau", 3, plural = "x", include_number = FALSE)
# ... or a new word can be substituted
verbaliseR::pluralise("sheep", 3, plural = "sheep", add_or_swap = "swap")
# Numbers below 1001 are written out in full at the start of sentences ...
verbaliseR::pluralise("penguin", 333, sentence_start = TRUE)
# ... but not in the middle of sentences
verbaliseR::pluralise("penguin", 333)
# Numbers above 1000 are never written out in full but are formatted for readability ...
verbaliseR::pluralise("penguin", 33333, sentence_start = TRUE)
# ... with a customisable big mark to allow for different conventions
verbaliseR::pluralise("penguin", 33333, big_mark = " ")
# Numbers with decimals are always left as numerals and aren't formatted
verbaliseR::pluralise("unit", 12345.67)
listify()
Takes a vector and returns a string where the items in the vector are listed in prose. The link word can be anything you like, and there is an option to add an Oxford comma if desired. Here are a few examples:
# to which you can choose add an Oxford comma
verbaliseR::listify(c("a", "b", "c"), oxford_comma = TRUE)
# ... and get quite creative with it ...
verbaliseR::listify(
c(verbaliseR::listify(c("a", "b", "c"), linking_word = "or"), "d"),
linking_word = "but most certainly not",
oxford_comma = TRUE
)
prettify_date()
Takes a date or string formatted as “YYYY_MM_DD” or “YYYY/MM/DD” and returns a string which is the date formatted in prose. Options include UK/US style and formal/informal (without / with the ordinals)
# Defaults to UK style, informal
verbaliseR::prettify_date(Sys.Date())
# Can also do US style
verbaliseR::prettify_date("2022/09/15", uk_or_us = "US")
# To remove the ordinals, select formal_or_informal = "formal"
verbaliseR::prettify_date(
"2022-09-15",
uk_or_us = "US",
formal_or_informal = "formal"
)
num_to_text()
Used within pluralise() this function can also be useful on its own. It takes a number (whole number as numeric or integer) and writes it out in full, applying the following rules:
- Numbers 0-10 are always written out in full, regardless of their place in the sentence
- Numbers 11-1000 are written out in full only if they are at the start of a sentence
- Negative numbers follow the same rules, with “minus” in front (e.g. -3 turns into “minus three”)
- Numbers above 1000 are never written out in full, but are formatted for readability with a big mark delimiter (e.g. 12345 turns into “12,345”)
- Numbers containing a decimal point are never written out in full, and are left as they are (e.g. 12345.67 stays “12345.67”)
The big mark can be modified.
verbaliseR::num_to_text(3)
# 0 defaults to "no", but can be changed to anything
verbaliseR::num_to_text(0)
verbaliseR::num_to_text(0, zero_or_no = "none")
verbaliseR::num_to_text(3, sentence_start = TRUE)
# Only whole numbers are returned as text; a warning is issued accordingly
verbaliseR::num_to_text(1.25, sentence_start = TRUE)
# Numbers greater than 1000 are not returned as text even if they are at the start of a sentence;
# a warning is issued accordingly
# They are however formatted for readability
verbaliseR::num_to_text(3333, sentence_start = TRUE)## Warning: Numbers greater than 1000 are returned as numerals, regardless of their place
## in the sentence.
## [1] "3,333"
# To change the default formatting, specify a custom big_mark
verbaliseR::num_to_text(3333, sentence_start = TRUE, big_mark = " ")
restore_capitals()
Takes a string in which some or all capitalisation has been lost, and restores capitals in the specified items.
x <- "Should i tell c-3po the french call him z-6po?"
verbaliseR::restore_capitals(x, c("I", "C-3PO", "French", "Z-6PO"))Working with data frames
Since version 0.2.0, num_to_text(), pluralise(), prettify_date() and restore_capitals() all work on whole vectors, so you can use them inside dplyr::mutate() to write a sentence for every row of your data. Handy for creating the content of tooltips! Missing values stay missing, so the odd NA won’t stop the function from being applied to the other rows.
palmerpenguins::penguins %>%
dplyr::count(species, island) %>%
dplyr::mutate(
sentence = paste0(
verbaliseR::pluralise("penguin", n, sentence_start = TRUE),
" from the ",
species,
" species ",
ifelse(n == 1, "lives", "live"),
" on ",
island,
"."
)
) %>%
dplyr::pull(sentence)## [1] "Forty-four penguins from the Adelie species live on Biscoe."
## [2] "Fifty-six penguins from the Adelie species live on Dream."
## [3] "Fifty-two penguins from the Adelie species live on Torgersen."
## [4] "Sixty-eight penguins from the Chinstrap species live on Dream."
## [5] "One hundred and twenty-four penguins from the Gentoo species live on Biscoe."listify() does the opposite: it squishes a whole vector into one string. That allows it to work nicely in conjunction with dplyr::summarise().
palmerpenguins::penguins |>
dplyr::group_by(species) |>
dplyr::summarise(
n_islands = dplyr::n_distinct(island),
islands = verbaliseR::listify(unique(island))
) %>%
dplyr::mutate(
sentence = paste0(
species,
" penguins can be found on the ",
verbaliseR::pluralise("island", n_islands, include_number = FALSE),
" of ",
islands,
"."
)
) |>
dplyr::pull(sentence)Further information
- Report bugs or suggest new features here.
- Open to PRs from rstats users who want to make
num_to_textandprettify_date()work in different languages. - Logo by Jenny Legrand
