`%format%` <- function(fmt, params) {
# Use sprintf() with named format strings
# https://stackoverflow.com/questions/17475803/sprintf-format-strings-reference-by-name/17476306
#
# Arguments:
# fmt {char} -- string with format string markers. Ex:
# '%(label)s: %(april)d %(may)d %(june)d'
# params {list} -- list of items to sub in for matching format string names Ex:
# list(label = "months", april = 4, may = 5, june = 6)
#
# Returns:
# char
library(gsubfn)
library(stringr)
pat = "%\\(([^)]*)\\)"
fmt2 = gsub(pat, "%", fmt)
# Make sure that all %()s are accounted for in 'params'
req_params = str_extract_all(fmt, pat)[[1]] %>% str_replace_all("(\\%|\\(|\\))", "") %>% unique()
if (length(unique(req_params)) > length(unique(names(params)))) {
missing_params = req_params[!(req_params %in% names(params))]
stop(sprintf("Missing parameter(s) in 'params' argument: %s",
paste0(missing_params, collapse = ", ")))
} else if (length(unique(req_params)) < length(unique(names(params)))) {
extra_params = names(params)[!(names(params) %in% req_params)]
stop(sprintf("Extra parameter(s) in 'params' argument: %s",
paste0(extra_params, collapse = ", ")))
}
list2 = params[strapplyc(fmt, pat)[[1]]]
# Check if fmt string is too long. If it is, break into chunks using sprintf_max() function
if (nchar(fmt) > 8192) {
# sprintf_max() function in rdoni
library(rdoni)
paste0(do.call("sprintf_max", c(5000, fmt2, list2)), collapse = "")
} else {
do.call("sprintf", c(fmt2, list2))
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.