Following on from this example. I have a function in R which generates some CSS code.
CSS <- function(values, colors){
template <- "
.option[data-value=%s], .item[data-value=%s]{
background: %s !important;
color: white !important;
}"
paste0(
apply(cbind(values, colors), 1, function(vc){
sprintf(template, vc[1], vc[1], vc[2])
}),
collapse = "\n"
)
}
For example, CSS(c("a","b","c"), c("red", "green", "blue"))
generates
.option[data-value=a], .item[data-value=a]{
background: red !important;
color: white !important;
}
.option[data-value=b], .item[data-value=b]{
background: green !important;
color: white !important;
}
.option[data-value=c], .item[data-value=c]{
background: blue !important;
color: white !important;
}
However, if my values are c("a 1","b 2","c 3 3")
and I execute CSS(c("a 1","b 2","c 3 3"), c("red", "green", "blue"))
the CSS does not recognise the space between each element, for each character in the character vector. So "a 1"
is not recognised, it is read as "a"
, and "b 2"
is read as "b"
etc.
I know I need to wrap each character in quotes, as otherwise the space will just become an attribute separator and everything after spaces will be seen as element attributes. But how do I do this within the above function without having to hardcode it?