Created
August 22, 2017 20:12
-
-
Save jrnold/019f7a5673ad23942fd4378482719a84 to your computer and use it in GitHub Desktop.
Write R object to pgfkeys
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
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