Skip to content

Instantly share code, notes, and snippets.

@jrnold
Created August 22, 2017 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrnold/019f7a5673ad23942fd4378482719a84 to your computer and use it in GitHub Desktop.
Save jrnold/019f7a5673ad23942fd4378482719a84 to your computer and use it in GitHub Desktop.
Write R object to pgfkeys
write_pgfkeys <- function(x, ...) {
UseMethod("write_pgfkeys")
}
.pgfkey <- function(key, value) {
str_c("\\pgfkeysetvalue{", key, "}{", value, "}")
}
write_pgfkeys.default <- function(x, path=NULL, root = "/data/", ...) {
keys <- str_c(root, names(x)) %>%
# ensure leading and trailing / and no //
str_replace("^/*", "/") %>%
str_replace("/*$", "/") %>%
str_replace("//", "/")
out <- str_c(map2_chr(keys, as.character(x), .pgfkey), collapse = "\n")
if (!is.null(path)) {
cat(out, file = path)
}
invisible(out)
}
write_pgfkeys.environment <- function(x, ....) {
v <- set_names(map_chr(names(x), function(i) as.character(x[[i]])),
names(x))
write_pgfkeys.character(v)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment