Is there a function to create character string of name-value pairs?

I have often had to write functions myself to create the facet titles or table titles for a combination of name-value pairs. See example below.

Is there a function somewhere that can do this? Perhaps with more options and generalizability?


to_label <- function(x) {
  map2(names(x), x, str_c, sep = "=") %>% str_c(collapse = ", ")

list(a = 1, b = "two", c = FALSE) %>% to_label()
#> [1] "a=1, b=two, c=FALSE"

Created on 2022-01-11 by the reprex package (v2.0.1)


As far as I know that doesn't exist, because if you'd use a list as in your example as input, the value could be anything, for example a data frame or nested list. Trying to collapse that into a vector would likely lead to issues.

Example that would lead to issues:

list(a = 1, b = "two", c = c(1, 2)) %>% to_label()


1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.