R/report.dmdScheme_validation.R

Defines functions report.dmdScheme_validation

Documented in report.dmdScheme_validation

#' Method for creating a report from an `dmdScheme_validation` object
#'
#' @details
#' **`report.dmdScheme_validation`** creates a report of the object returnes from a `validate()`.
#'
#' @importFrom utils browseURL
#' @importFrom rmarkdown render
#'
#' @export
#' @md
#' @describeIn report report of a `dmdScheme_validation` object.
#'
#' @examples
#' ## Report of `dmdScheme_validation`
#' \dontrun{
#' # This needs pandoc to run successfully
#' report( validate(dmdScheme_raw()) )
#' report(
#'    x = dmdScheme_raw(),
#'    report = "html",
#'    report_author = "The Author I am",
#'    report_title = "A Nice Report"
#' )
#' }
report.dmdScheme_validation <- function(
  x,
  file = tempfile(),
  open = TRUE,
  report = "html",
  report_author = "Tester",
  report_title = "Validation of data against dmdScheme",
  ...
) {
  if (length(report) != 1) {
    stop("'report' has to be exactly of length 1!")
  }

  allowedFormats <- c("html", "pdf", "word")
  if (!(report %in% allowedFormats)) {
    stop("'report' has to be one of the following values: (", paste(allowedFormats, collapse = ', '), ")!")
  }

  if (length(report) > 1) {
    stop("The length of the argument `report` has to be exactly one!")
  }

  # Generate report ---------------------------------------------------------


  temppath <- tempfile()
  dir.create(temppath, recursive = TRUE, showWarnings = FALSE)
  tempReport <- file.path(temppath, "validation_report.Rmd")
  file.copy(
    from = system.file("reports", "validation_report.Rmd", package = "dmdScheme"),
    to = tempReport,
    overwrite = TRUE
  )

  result <- rmarkdown::render(
    input = tempReport,
    output_format = paste0(report, "_document"),
    params = list(
      author = report_author,
      title = report_title,
      x = x,
      result = x
    )
  )

  file.copy(
    from = result,
    to = file
  )

  # Open report -------------------------------------------------------------

  if (open) {
    if (interactive()) {
      utils::browseURL(
        url = result,
        encodeIfNeeded = TRUE
      )
    } else {
      message("Non-interactive session. If interactive session, the report would be opened.")
    }
  }

  # Return result -----------------------------------------------------------

  return(file)
}

Try the dmdScheme package in your browser

Any scripts or data that you put into this service are public.

dmdScheme documentation built on Aug. 22, 2022, 9:06 a.m.