# Don't Edit This:
subobj <- ifelse(exists("subobj"), subobj, FALSE) # create a subobj letter (1a)
newobj <- ifelse(exists("newobj"), newobj, TRUE) # force a new object number (1b-> 2a and 1 -> 2)
cnt_chapt_content<-NMFSReports::auto_counter(cnt_chapt_content)
cnt_figures<-ifelse(newobj, cnt_figures+1, cnt_figures)

if (subobj) {
  # if it contains letters
  if (newobj) {
    cnt_figures_sub <- cnt_figures_sub0 <- letters[1]
  } else {
    # cnt_figures_sub <- letters[which(letters == 
    #                                   gsub("[^a-zA-Z]", "", list_figures[length(list_figures)][[1]]$number))+1]
    cnt_figures_sub <- cnt_figures_sub0 <- letters[which(letters == cnt_figures_sub0)+1]
  }
} else {
  cnt_figures_sub <- ""
}

if (!(exists("table_raw"))) {
  table_raw <- NULL
}

if (!exists("cnt_pre")) {
  cnt_pre <- ""
# } else {
#   cnt_pre <- paste0(cnt_pre, " ")
}


# Systematically save your plot with this function
list_figures<-NMFSReports::save_figures(
  figure = figure, 
  raw = table_raw, 
  list_figures = list_figures, 
  header = ifelse(exists("header", mode = "character"), header, ""),
  footnotes = unlist(ifelse(exists("footnotes", mode = "character"), list(footnotes), "")), 
  alttext = ifelse(exists("alttext", mode = "character"), alttext, header),
  filename0 = ifelse(exists("filename0", mode = "character"), filename0, nickname), 
  nickname = ifelse(exists("nickname", mode = "character"), nickname, filename0),
  filename_desc = ifelse(exists("filename_desc", mode = "character"), filename_desc, nickname),
  cnt_chapt_content = ifelse(exists("cnt_chapt_content", mode = "character"), cnt_chapt_content, "001"), 
  width = ifelse(exists("width", mode = "numeric"), width, 6), 
  height = ifelse(exists("height", mode = "numeric"), height, 6),
  cnt = paste0(cnt_pre, cnt_figures, cnt_figures_sub), 
  path = dir_out_figures)
# fig.cap = ifelse(indesign_flowin %in% TRUE, list_figures[[length(list_figures)]]$alttext, paste0("\n#### *", list_figures[[length(list_figures)]]$header, "*")), 

# Print or Don't Print Plot in Text
# You don't want to print this in the document if this text will be flowed into InDesign. 
# However, sometimes its nice to see everything all together, so this variable is 
# something you might like to toggle on and off. 
# Hense, FALSE = print here, TRUE = don't print here, just make the .pdf (coded above)
if (indesign_flowin %in% FALSE) {

  if (!(exists("usePNGPDF"))) {
   list_figures[[length(list_figures)]]$figure # print plot in text
  } else if (usePNGPDF == "png") {
    knitr::include_graphics(path = paste0("..",gsub(pattern = dir_in, replacement = "", x = dir_out_figures),
                                 list_figures[nickname][[1]]$filename,".png"), 
                            error = FALSE)
    # cowplot::ggdraw() +
    #       cowplot::draw_image(image = paste0(dir_out_figures, list_figures[nickname][[1]]$filename,".png"))
  # } else if (usePNGPDF == "pdf") {
    # cowplot::ggdraw() +
    #   draw_image(magick::image_read_pdf(path = paste0(dir_out_figures, 
    #                              list_figures[nickname][[1]]$filename,".pdf"),
    #                              density = 600))
    # cowplot::ggdraw() +
    #       cowplot::draw_image(image = paste0(dir_out_figures, list_figures[nickname][[1]]$filename,".pdf"))
    # knitr::include_graphics(path = paste0(dir_out_figures,
    #                              list_figures[nickname][[1]]$filename,".pdf"))
  }
} else if (indesign_flowin %in% TRUE){ # for reports that need to be flowed into InDesign
  Title0 <- ifelse(indesign_flowin %in% TRUE, "", list_figures[[length(list_figures)]]$caption)
  rmarkdown::render(system.file("rmd/_TableFigureHeader.Rmd", package = "NMFSReports"),
                    quiet = TRUE,
                    output_dir = dir_chapters,
                    output_file = paste0(filename00,cnt_chapt_content,"_Title.docx"))
}

# make sure you dont mistakenly name other files with these names
remove_who <- c()
remove_who0 <- c("figure", "table_raw", "header", "footnotes", "subobj", "newobj", #"nickname", 
                 "filename_desc", "alttext", "width", "height", "cnt_pre")
for (i in 1:length(remove_who0)){
  if(exists(remove_who0[i]) & !exists(remove_who0[i], mode = "function")){
    remove_who <- c(remove_who, remove_who0[i])
  }
}
remove(list = remove_who)

r ifelse(indesign_flowin %in% TRUE, list_figures[[length(list_figures)]]$alttext, paste0("#### ", list_figures[[length(list_figures)]]$header))




EmilyMarkowitz-NOAA/NMFSReports documentation built on March 26, 2023, 1:08 a.m.