Created
April 25, 2019 03:12
-
-
Save anfederico/afc5eb051768c836b6d4c4e6621836b2 to your computer and use it in GitHub Desktop.
Python style string formatting in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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