Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Last active November 16, 2018 15:30
Show Gist options
  • Save hrbrmstr/8c7862092681d06e6535ab38d03074bf to your computer and use it in GitHub Desktop.
Save hrbrmstr/8c7862092681d06e6535ab38d03074bf to your computer and use it in GitHub Desktop.
as_tribble <- function(x) {
out <- capture.output(write.csv(x, quote=TRUE, row.names=FALSE))
out[1] <- gsub('"', '`', out[1])
out[1] <- sub('^`', " ~`", out[1])
out[1] <- gsub(',`', " , ~`", out[1])
out <- paste0(out, collapse=",\n ")
cat(sprintf("tribble(\n%s\n)", out, ")\n"))
}
as_tribble(head(mtcars))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment