R/tidy_ggedit_code.R

Defines functions tidy_ggedit_code

Documented in tidy_ggedit_code

# x - ggedit object
#' Extract and tidy code from ggedit object.
#'
#' @param x A `ggedit` object.
#' @param ... Other parameters to [styler::style_text()].
#'
#' @return String.
#' @export
tidy_ggedit_code <- function(x, ...) {
  if (!inherits(x, "ggedit")) stop("Not ggedit object")

  field_names <- grep("Calls", names(x), value = TRUE)

  calls <- sapply(field_names, function(y) {
    sapply(x[[y]], function(obj) {
      paste0(obj, collapse = " + \n")
    })
  })

  if (is.null(dim(calls))) {
    nm <- names(calls)
    calls <- matrix(calls, nrow = 1)
    rownames(calls) <- unique(gsub("^[^.]*.", "", nm))
  }

  out <- apply(calls, 1, function(obj) {
    paste(obj[!obj %in% c("list()", "")], collapse = " + \n")
  })

  out <- styler::style_text(out, ...)
  out
}
GegznaV/BioStat documentation built on Aug. 14, 2020, 9:30 p.m.