R/add_captions.R

Defines functions add_captions

Documented in add_captions

#' @title Touch up figure and table captions after using `convert_docx_to_rmd()`
#'
#' @description After using `convert_docx_to_rmd()`, figure and table captions
#' are part of the main text and cross references to them are just text and
#' numbers, making it difficult to integrate these `.Rmd` files in reports:
#' they are not automatically renumbered, these captions cannot be listed,...
#' This function replaces the figure and table descriptions with true captions,
#' and cross references to these figures and tables with dynamic references
#' (that refer to the figure or table label).
#'
#'
#' @details This function expects an input that is generated by
#' `convert_docx_to_rmd()`, with \itemize{
#'   \item  figure captions below the figure and starting with `Figuur `
#'   (or other name to be defined in variable `name_figure`),
#'   then a unique number and finally the description in one paragraph
#'   \item  table captions above the table and starting with `Tabel `
#'   (or other name to be defined in variable `name_table`),
#'   then a unique number and finally the description in one paragraph
#'   \item figures consisting of one image, not 2 figures near each other
#'   \item cross references having the same label as the figure or table they
#'   refer to, default `Figuur ` or `Tabel ` followed by the unique number
#'   written in exactly the same way
#' }
#' Please check carefully for mistakes after using this function
#'
#' @param from The `.Rmd` file to convert.
#' Can be given as an absolute or relative path.
#' @param to The filename to write the resulting `.Rmd` file.
#' Can be given as an absolute or relative path.
#' @param name_figure_from name that is given to figures in captions and cross
#' references in the `.Rmd` provided in `from` (default is `Figuur`)
#' @param name_table_from name that is given to tables in captions and cross
#' references in the `.Rmd` provided in `from` (default is `Tabel`)
#' @param name_figure_to name that should be given to figures in cross
#' references in the output `.Rmd` (default is `Figuur`)
#' @param name_table_to name that should be given to tables in cross
#' references in the output `.Rmd` (default is `Tabel`)
#'
#' @importFrom assertthat is.string
#'
#' @export
#' @family convert
#'
add_captions <- function(
  from,
  to,
  name_figure_from = "Figuur",
  name_table_from = "Tabel",
  name_figure_to = "Figuur",
  name_table_to = "Tabel") {

  # input checks
  assert_that(!missing(from))
  from <- normalizePath(from)
  assert_that(endsWith(from, ".Rmd"))
  assert_that(!missing(to), is.string(to))
  assert_that(dir.exists(dirname(to)))
  assert_that(endsWith(to, ".Rmd"))
  assert_that(is.string(name_figure_from))
  assert_that(is.string(name_table_from))
  assert_that(is.string(name_figure_to))
  assert_that(is.string(name_table_from))

  text <- readLines(from, encoding = "UTF-8")
  text_1string <- paste(text, collapse = "\n")
  text_1string <- gsub(pattern = "\\n\\n(\\n)+", "\n\n", text_1string)
  # replace figure caption
  split_string <- c(unlist(strsplit(text_1string, split = "\\!\\[")))
  split_string <-
    gsub(
      pattern =
        sprintf(
          "^(.+)?\\]\\((.*)\\)\\{([^\\}]*)\\}\\n\\n\\*?\\*?(%s) (\\d+(\\D\\d+)?)[.:]?\\*?\\*? \\*?\\*?(.+?)\\*?\\*?\\n\\n(.*)", #nolint
          name_figure_from
        ),
      replacement = "(#fig:\\4\\5) \\7](\\2){\\3}\n\n\\8", #nolint
      x = split_string
    )
  text_1string <- paste(split_string, collapse = "![")
  # replace figure reference
  text_1string <-
    gsub(
      pattern =
        sprintf("([^\\#fig\\:])(%s) (\\d+(\\D\\d+)?)", name_figure_from),
      replacement = sprintf("\\1%s \\\\@ref(fig:\\2\\3)", name_figure_to),
      x = text_1string
    )
  # replace figure reference met line ending
  text_1string <-
    gsub(
      pattern =
        sprintf("([^\\#fig\\:])(%s)\\n(\\d+(\\D\\d+)?)", name_figure_from),
      replacement = sprintf("\\1%s\n\\\\@ref(fig:\\2\\3)", name_figure_to),
      x = text_1string
    )

  # replace table caption
  split_string <- c(unlist(strsplit(text_1string, split = "-----")))
  split_string <-
    gsub(
      pattern =
        sprintf(
          "(.*)\\n\\n\\*?\\*?(%s) (\\d+(\\D\\d+)?)[.:]?\\*?\\*? \\*?\\*?(.+?)\\*?\\*?\\n\\n", #nolint
          name_table_from
        ),
      replacement = "\\1\n\nTable: (#tab:\\2\\3) \\5\n\n",
      x = split_string
    )
  text_1string <- paste(split_string, collapse = "-----")

  # replace table reference
  text_1string <-
    gsub(
      pattern = sprintf("([^\\#tab\\:])(%s) (\\d+(\\D\\d+)?)", name_table_from),
      replacement = sprintf("\\1%s \\\\@ref(tab:\\2\\3)", name_table_to),
      x = text_1string
    )
  # replace table reference with line ending
  text_1string <-
    gsub(
      pattern =
        sprintf("([^\\#tab\\:])(%s)\\n(\\d+(\\D\\d+)?)", name_table_from),
      replacement = sprintf("\\1%s\n\\\\@ref(tab:\\2\\3)", name_table_to),
      x = text_1string
    )
  text2 <- c(unlist(strsplit(text_1string, split = "\\n")), "")
  con <- file(to, encoding = "UTF-8")
  writeLines(text2, con = con)
  close(con)
  return(invisible(NULL))
  }
inbo/protocolshelper documentation built on Sept. 6, 2024, 9:15 p.m.