knitr::opts_chunk$set(
  comment = "##",
  tidy = FALSE, #`styler` to use styler:style_text() to reformat code
  tidy.opts = list(blank = FALSE, width.cutoff = 60),
  echo = TRUE,
  eval = TRUE,
  cache = FALSE,
  child = NULL, #file/s to knit and then include,
  collapse = FALSE, #collapse all output into a single block,
  error = FALSE, #display error messages in doc. FALSE stops render when error is thrown
  fig.align = "center", #left, right, center, or default
  fig.width = 7, #inches
  fig.height = 7, #inches
  fig.asp=0.50, #adds whitespace around images
  include = TRUE, #include chunk?
  message = TRUE, #display code messages?
  warning = TRUE, #include warnings?
  results = "markup"
    # "asis": passthrough results
    # "hide": do not display results 
    # "hold": put all results below all code
)  

knit_print.data.frame = function(x, ...) {
  res = DT::datatable(x)
  knitr::knit_print(res)
}

registerS3method(
  "knit_print", "data.frame", knit_print.data.frame,
  envir = asNamespace("knitr")
)
knitr::knit_hooks$set(error = function(x, options) { 
  paste(c('\n\n:::{style="color:Crimson; background-color:SeaShell;"}',
        gsub('^## Error', '**Error**:', x),
        ':::\n'), collapse = '\n')
})

knitr::knit_hooks$set(warning = function(x, options) { 
  paste(c('\n\n:::{style="color:Gray; background-color:White;"}',
        gsub('^## Warning', '**Warning**:', x), ':::\n'), collapse = '\n')
})

knitr::knit_hooks$set(message = function(x, options) { 
  paste(c('\n\n:::{style="color:Green; background-color:PaleGreen;"}',
        gsub('^## Message', '**Message**:', x), ':::\n'), collapse = '\n')
})


knitr::knit_hooks$set(timed = local({
  now <- NULL
  function(before, options) {
    if (before) {
      # record the current time before each chunk
      now <<- Sys.time()
    } else {
      # calculate the time difference after a chunk
      res <- difftime(Sys.time(), now)
      # return a character string to show the time
      paste("Time for this code chunk to run:", res)
    }
  }
}))
# If the session is interactive, the Rmd title is 
# checked against the filename and the parameterized value, which should be the same.
  if (interactive()) { 
    rmd_title_by_filename <- xfun::sans_ext(basename(rstudioapi::getSourceEditorContext()$path)) 
    rmd_title_by_params <- 
        sprintf("%s: %s", 
            gsub(pattern = "^'|'$", 
                 x = params$issue_key,
                 replacement = ""),
            params$report_title)
     if (rmd_title_by_filename != rmd_title_by_params) {
       stop("Filename does not match parameterized name")
     } else {
     rmd_title <- rmd_title_by_filename
     }

  } else {
  rmd_title <- 
    sprintf("%s: %s", 
            gsub(pattern = "^'|'$", 
                 x = params$issue_key,
                 replacement = ""),
            params$report_title)
  }

github_page  <- sprintf("%s/%s.html", params$github_page_path, rmd_title)
source_code  <- sprintf("%s/%s.Rmd", params$source_code_page_path, rmd_title)
issue_key    <- params$issue_key
report_title <- params$report_title
project_path <- path.expand(params$project_path)

Last Updated On: r Sys.time()

library(tidyverse)
library(easyBakeOven)
library(glue)
global_data_folder  <- file.path(project_path, "data", issue_key, report_title)
raw_folder          <- file.path(global_data_folder, "raw")
intermediate_folder <- file.path(global_data_folder, "intermediate")
final_folder        <- file.path(global_data_folder, "final")
outgoing_folder     <- file.path(global_data_folder, "outgoing") 


global_rmd_folder   <- file.path(project_path, "rmd", issue_key)
child_rmd_folder    <- file.path(global_rmd_folder, report_title)

global_img_folder   <- file.path(project_path, "img", issue_key)
img_folder          <- file.path(global_img_folder, report_title)
cache_folder        <- file.path(project_path, "cache", issue_key, report_title, "/")

sapply(c(global_data_folder,
         raw_folder,
         intermediate_folder,
         final_folder,
         outgoing_folder,
         global_rmd_folder,
         child_rmd_folder,
         global_img_folder,
         img_folder,
         cache_folder),
       create_path)

knitr::opts_chunk$set(
  cache.path = cache_folder
)  


meerapatelmd/suzyBakeOven documentation built on March 30, 2022, 1:44 p.m.