Skip to content

Instantly share code, notes, and snippets.

@daigotanaka
Last active January 11, 2017 05:26
Show Gist options
  • Save daigotanaka/782fda7e652ac7f943b10ba03060524e to your computer and use it in GitHub Desktop.
Save daigotanaka/782fda7e652ac7f943b10ba03060524e to your computer and use it in GitHub Desktop.
sprintf that takes key-value inputs in the format
# %format% by G. Grothendieck from http://stackoverflow.com/a/17476306/3423480
library(gsubfn)
`%format%` <- function(fmt, list) {
pat <- "%\\(([^)]*)\\)"
fmt2 <- gsub(pat, "%", fmt)
list2 <- list[strapplyc(fmt, pat)[[1]]]
do.call("sprintf", c(fmt2, list2))
}
sprintf_ <- function(format, ...) {
inputs <- list(...)
if (is.list(inputs[[1]])) {
return (format %format% inputs[[1]])
}
return (sprintf(format, ...))
}
sprintf_.test <- function() {
# If an arg list is given, function as normal sprintf
print(sprintf_("%s: %s", "author", "Daigo Tanaka"))
# If the param is given as a list, work like
# print("%(key)s: %(value)d" % {"key": "rank", "value": 1})
# in Python
print(sprintf_("%(key)s: %(value)d", list(key="rank", value=1)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment