#' @importFrom scales label_bytes
create_byte_formatter <- scales::label_bytes
#' @importFrom scales label_dollar
create_dollar_formatter <- scales::label_dollar
#' @importFrom scales label_comma
create_decimal_formatter <- scales::label_comma
#' @importFrom scales label_scientific
create_scientific_formatter <- scales::label_scientific
#' @importFrom scales label_percent
create_percent_formatter <- scales::label_percent
#' @export
#' @importFrom R6 R6Class
#' @importFrom tibble tibble
#' @importFrom readr write_lines
#' @importFrom purrr map2_chr
#' @importFrom utils packageVersion
LatexMacroGenerator <-
R6::R6Class("LatexMacroGenerator",
public = list(
initialize = function(filepath,
append = FALSE,
header = TRUE,
byte_formatter = create_byte_formatter(),
dollar_formatter = create_dollar_formatter(),
decimal_formatter = create_decimal_formatter(),
scientific_formatter = create_scientific_formatter(),
percent_formatter = create_percent_formatter()) {
private$filepath <- filepath
private$byte_formatter <- byte_formatter
private$dollar_formatter <- dollar_formatter
private$decimal_formatter <- decimal_formatter
private$scientific_formatter <- scientific_formatter
private$percent_formatter <- percent_formatter
if (!append) {
cat("", private$filepath, append = FALSE)
}
if (header) {
header_text <-
str_glue("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
"%% AUTOGENERATED BY papr-{version} (https://github.com/PRL-PRG/papr)",
"%% DO NOT EDIT MANUALLY",
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
version = packageVersion('papr'),
.sep = "\n")
cat(header_text, private$filepath, append = append)
}
invisible(self)
},
get_byte_formatter = function() {
private$byte_formatter
},
set_byte_formatter = function(byte_formatter) {
private$byte_formatter <- byte_formatter
},
get_dollar_formatter = function() {
private$dollar_formatter
},
set_dollar_formatter = function(dollar_formatter) {
private$dollar_formatter <- dollar_formatter
},
get_decimal_formatter = function() {
private$decimal_formatter
},
set_decimal_formatter = function(decimal_formatter) {
private$decimal_formatter <- decimal_formatter
},
get_scientific_formatter = function() {
private$scientific_formatter
},
set_scientific_formatter = function(scientific_formatter) {
private$scientific_formatter <- scientific_formatter
},
get_percent_formatter = function() {
private$percent_formatter
},
set_percent_formatter = function(percent_formatter) {
private$percent_formatter <- percent_formatter
},
byte = function(name, value, formatter = self$get_byte_formatter()) {
private$process_macros(name, value, formatter)
},
dollar = function(name, value, formatter = self$get_dollar_formatter()) {
private$process_macros(name, value, formatter)
},
decimal = function(name, value, formatter = self$get_decimal_formatter()) {
private$process_macros(name, value, formatter)
},
scientific = function(name, value, formatter = self$get_scientific_formatter()) {
private$process_macros(name, value, formatter)
},
percent = function(name, value, formatter = self$get_percent_formatter()) {
private$process_macros(name, value, formatter)
},
names = function(template, prefixes = "", suffixes = str_to_upper(letters[1:length(template)])) {
paste(prefixes, template, suffixes)
}
),
private = list(
filepath = NA,
byte_formatter = NULL,
dollar_formatter = NULL,
decimal_formatter = NULL,
scientific_formatter = NULL,
percent_formatter = NULL,
create_macros = function(name, definition) {
representation <- map2_chr(name,
definition,
function(name, definition) {
sprintf("\\newcommand{\\%s}{%s\\xspace}", name, definition)
})
macros <- tibble(name = name, definition = definition, representation = representation)
structure(macros, class = c("latex_macro", class(macros)))
},
write_macros = function(macros) {
write_lines(macros$representation, private$filepath, append = TRUE)
macros
},
process_macros = function(names, values, formatter) {
definitions <- formatter(values)
macros <- private$create_macros(names, definitions)
private$write_macros(macros)
}
)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.