Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sprintf format strings: reference by name?

Tags:

r

In Python, you can specify string formats by name (this is of course a silly example):

parameters = {'label':'months', 'april':4,'may':5,'june':6}
formatstring = '%(label)s: %(april)d %(may)d %(june)d'
outputstring = formatstring % parameters

(The formatstring % parameters notation is the Python equivalent to do.call(sprintf,c(formatstring,as.list(parameters))) in R.)

The output string would be "months: 4, 5, 6". parameters is stored as a key-value pair (which might be called a dictionary, hashtable, or named list in various languages). The string format %(text)s allows you to reference which dictionary item (text) should be formatted in that slot.

Is there anything equivalent in R, or you have you found a good workaround?

like image 205
hatmatrix Avatar asked Jul 04 '13 17:07

hatmatrix


People also ask

Does Sprintf overwrite the string?

The sscanf () function and the sprintf () function are like the two sides of a coin. You can now use the sprintf() function to reassemble the string. You can use the same char array stringa- its previous value gets overwritten. Try it out for yourself to get a better grasp on it.

What is sprintf () function in C?

sprintf() in C sprintf stands for "string print". In C programming language, it is a file handling function that is used to send formatted output to the string. Instead of printing on console, sprintf() function stores the output on char buffer that is specified in sprintf.

What is the point of Sprintf?

Definition and Usage The sprintf() function writes a formatted string to a variable.

What is Sprintf %D?

Character specifiers For example, if you use %d, you're telling sprintf() to format the inserted variable as a signed decimal integer. You may be wondering what it means when a character is defined as a signed decimal integer. Here's the scoop: Signed means it can be positive or negative.


2 Answers

1. Try gsubfn in the gsubfn package:

library(gsubfn)
parameters <- list(label = "months", april = 4, may = 5, june = 6)

gsubfn("\\w+", parameters, "label: april, may, june")

2. or try fn$ from the same package:

with(parameters, fn$identity("$label: $april, $may, $june"))

3. Here is a short infix function that transforms a format string and a list of parameters to a sprintf and then runs it:

library(gsubfn)
`%format%` <- function(fmt, list) {
    pat <- "%\\(([^)]*)\\)"
    fmt2 <- gsub(pat, "%", fmt)
    list2 <- list[strapplyc(fmt, pat)[[1]]]
    do.call("sprintf", c(fmt2, list2))
}

Use it like this:

> '%(label)s: %(april)d %(may)d %(june)d' %format% parameters
[1] "months: 4 5 6"
like image 151
G. Grothendieck Avatar answered Oct 18 '22 01:10

G. Grothendieck


Although this is not built in to the system sprintf functions that R is using (see man printf for the system docs), it's easy enough to implement such a feature in R by replacing the named references with their respective positions -

sprintf_named <- function(fmt, ...) {
  args <- list(...)
  argn <- names(args)
  if(is.null(argn)) return(sprintf(fmt, ...))

  for(i in seq_along(args)) {
    if(argn[i] == "") next;
    fmt <- gsub(sprintf("%%{%s}", argn[i]), sprintf("%%%d$", i), fmt, fixed = TRUE)
  }

  do.call(sprintf, append(args, fmt, 0))
}

Here is an example usage:

sprintf_named("%{HIA}s!! %{RYLAH}s", RYLAH="Rock You Like a Hurricane", HIA="Here I Am")
## [1] "Here I Am!! Rock You Like a Hurricane"

We could also make it an infix:

`%format%` <- function(left, right) do.call(sprintf_named, append(right, left, 0))

"%{b}s %{a}s" %format% list(a='ya', b='boo')
## [1] "boo ya"
like image 38
Neal Fultz Avatar answered Oct 18 '22 00:10

Neal Fultz