Skip to content

Instantly share code, notes, and snippets.

@Keiku
Last active February 9, 2017 08:33
Show Gist options
  • Save Keiku/ede6e071c267589dd20fb518fbcc626b to your computer and use it in GitHub Desktop.
Save Keiku/ede6e071c267589dd20fb518fbcc626b to your computer and use it in GitHub Desktop.
Generate c() function code.
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