1
###########################################################################/**
2
# @RdocDefault compileKnitr
3
#
4
# @title "Compiles a Knitr file"
5
#
6
# \description{
7
#  @get "title".
8
# }
9
#
10
# @synopsis
11
#
12
# \arguments{
13
#   \item{filename, path}{The filename and (optional) path of the
14
#      document to be compiled.}
15
#   \item{...}{Additional arguments passed to @see "compileLaTeX".}
16
#   \item{outPath}{The output and working directory.}
17
#   \item{postprocess}{If @TRUE, and a postprocessing method exists for
18
#      the generated product, it is postprocessed as well.}
19
#   \item{verbose}{See @see "R.utils::Verbose".}
20
# }
21
#
22
# \value{
23
#   Returns the pathname of the generated document.
24
# }
25
#
26
# @author
27
#
28
# \seealso{
29
#   Internally, @see "knitr::knit" is used.
30
# }
31
#
32
# @keyword file
33
# @keyword IO
34
# @keyword internal
35
#*/###########################################################################
36
setMethodS3("compileKnitr", "default", function(filename, path=NULL, ..., outPath=".", postprocess=TRUE, verbose=FALSE) {
37 1
  use("knitr", quietly=TRUE)
38
  # To please R CMD check
39 1
  knit <- NULL; rm(list="knit")
40

41
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42
  # Validate arguments
43
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44
  # Arguments 'filename' & 'path':
45 1
  pathname <- if (is.null(path)) filename else file.path(path, filename)
46 1
  if (!isUrl(pathname)) {
47 1
    pathname <- Arguments$getReadablePathname(pathname)
48
  }
49

50
  # Arguments 'outPath':
51 1
  outPath <- Arguments$getWritablePath(outPath)
52 1
  if (is.null(outPath)) outPath <- "."
53

54
  # Argument 'verbose':
55 1
  verbose <- Arguments$getVerbose(verbose)
56 1
  if (verbose) {
57 1
    pushState(verbose)
58 1
    on.exit(popState(verbose))
59
  }
60

61 1
  verbose && enter(verbose, "Compiling Knitr document")
62
  # Download URL?
63 1
  if (isUrl(pathname)) {
64 0
    verbose && enter(verbose, "Downloading URL")
65 0
    url <- pathname
66 0
    verbose && cat(verbose, "URL: ", url)
67 0
    pathname <- downloadFile(url, verbose=less(verbose,50))
68 0
    verbose && cat(verbose, "Local file: ", pathname)
69 0
    verbose && exit(verbose)
70
  }
71

72 1
  pathname <- getAbsolutePath(pathname)
73 1
  verbose && cat(verbose, "Pathname (absolute): ", pathname)
74 1
  verbose && printf(verbose, "Input file size: %g bytes\n", file.info(pathname)$size)
75 1
  verbose && cat(verbose, "Output and working directory: ", outPath)
76

77 1
  opwd <- "."
78 1
  on.exit(setwd(opwd), add=TRUE)
79 1
  if (!is.null(outPath)) {
80 1
    opwd <- setwd(outPath)
81
  }
82

83 1
  pathname2 <- knit(pathname)
84 1
  pathname2 <- getAbsolutePath(pathname2)
85 1
  setwd(opwd); opwd <- "."
86

87 1
  res <- RspFileProduct(pathname2)
88 1
  verbose && print(verbose, res)
89

90
  # Postprocess?
91 1
  if (postprocess) {
92 0
    res <- process(res, outPath=outPath, recursive=TRUE, verbose=verbose)
93
  }
94

95 1
  verbose && exit(verbose)
96

97 1
  res
98
}) # compileKnitr()

Read our documentation on viewing source code .

Loading