R/05_unexported_helpers.R

formula_to_chunk <- function(x){
  lhs <- lhs(x,"call")
  rhs <- rhs(x,"character")
  chunk_header <-
    if (is.null(lhs)) "{r}" else
      glue("{{r, {quoted_list_to_arg_string(lhs)}}}")
  glue("```{chunk_header}\n{rhs}\n```")
}

# quoted_list_to_arg_string(quote(list(a=1, b=c)))
quoted_list_to_arg_string <- function(x) {
  paste(names(x[-1]),"=",x[-1],collapse = ", ")
}

# x <- "x x {x x} \n```{y {z}}\n x x\n```\n\n x x ```{x x} x x```"
# cat(x)
# cat(escape_chunk_header_braces(x))
# escape_chunk_header_braces <- function(x){
#   gsub("((^|\\n)```.*?)(\\{+)(.*?)(\\}+\n)(.*)",
#        "\\1{{\\4\\}}\\6",
#        x)
# }

code_to_chunk <- function(x){
  chr   <- paste(as.character(x),collapse = "\n")
  chunk <- paste0("```{r}\n",chr, "\n```")
}
moodymudskipper/mmmd documentation built on May 15, 2019, 9:14 p.m.