Last active
February 9, 2017 08:33
-
-
Save Keiku/ede6e071c267589dd20fb518fbcc626b to your computer and use it in GitHub Desktop.
Generate c() function code.
This file contains hidden or 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
library(stringr) | |
add_backquotes <- function(x) paste0("`", x, "`") | |
add_doublequotes <- function(x) paste0("\"", x, "\"") | |
generate_c_code <- function(x){ | |
vec <- paste0(add_doublequotes(x), sep=",\n") | |
vec_tail <- str_replace(tail(vec, 1), ",\n", "\n") | |
vec_head <- head(vec, length(vec) - 1) | |
vec <- c(vec_head, vec_tail) | |
cat("c(\n", vec, ")", sep="") | |
} | |
generate_c_code(names(iris)) | |
# c( | |
# "Sepal.Length", | |
# "Sepal.Width", | |
# "Petal.Length", | |
# "Petal.Width", | |
# "Species" | |
# ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment