R/Read.R

Defines functions Read

Documented in Read

#' Read Module
#'
#' Read creates a new module with one method: 'auto'. This method selects an
#' appropriate procedure for reading in the content of the file depending on the
#' file extension. It should not be necessary to call this function
#' interactively, please go to the docs for \link{newPresentation}.
#'
#' @seealso \link{newPresentation}
#'
#' @export
Read <- function() {
  modules::module({

    modules::export("auto")

    auto <- function(fileName, ...) {
      fileName <- normalizePath(fileName, mustWork = TRUE)
      fileExt <- tolower(tools::file_ext(fileName))
      stopifnot(fileExt %in% c("plain", "md", "rmd"))
      get(fileExt, mode = "function")(fileName, ...)
    }

    plain <- function(fileName, ...) {
      stopifnot(file.exists(fileName))
      readLines(fileName)
    }

    md <- function(fileName, ...) {
      stopifnot(file.exists(fileName))
      plainFile <- knitr::pandoc(fileName, format = "plain")
      plain(plainFile)
    }

    rmd <- function(fileName, ...) {
      stopifnot(file.exists(fileName))
      regex <- sprintf("%s$", tools::file_ext(fileName))
      mdFile <- sub(regex, "md", fileName)
      knitr::knit(fileName, mdFile, ...)
      md(mdFile)
    }

  })
}

Try the REPLesentR package in your browser

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

REPLesentR documentation built on Dec. 1, 2019, 1:19 a.m.