Skip to content

Instantly share code, notes, and snippets.

@anfederico
Created April 25, 2019 03:12
Show Gist options
  • Save anfederico/afc5eb051768c836b6d4c4e6621836b2 to your computer and use it in GitHub Desktop.
Save anfederico/afc5eb051768c836b6d4c4e6621836b2 to your computer and use it in GitHub Desktop.
Python style string formatting in R
example <- "An example {1} where adding {2} and {2} and {3}"
val.1 <- "Value 1"
val.2 <- "Value 2"
val.3 <- "Value 3"
format_str <- function(string, ...) {
args <- list(...)
for (i in 1:length(args)) {
pattern <- paste("\\{", i, "}", sep="")
replacement <- args[[i]]
string <- gsub(pattern, replacement, string)
}
return(string)
}
print(format_str(example, val.1, val.2, val.3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment