HenrikBengtsson / R.rsp
1
###########################################################################/**
2
# @RdocDefault typeOfRnw
3
#
4
# @title "Checks whether an Rnw file is a Sweave or 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 Rnw file.}
14
#   \item{default}{A @character string specifying the default result.}
15
#   \item{...}{Not used.}
16
# }
17
#
18
# \value{
19
#   Returns a @character string.
20
# }
21
#
22
# @author
23
#
24
# \seealso{
25
#   TBA.
26
# }
27
#
28
# @keyword file
29
# @keyword IO
30
# @keyword internal
31
#*/###########################################################################
32
setMethodS3("typeOfRnw", "default", function(filename, path=NULL, default="application/x-sweave", ...) {
33
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34
  # Validate arguments
35
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36
  # Arguments 'filename' & 'path':
37 1
  pathname <- if (is.null(path)) filename else file.path(path, filename)
38 1
  if (!isUrl(pathname)) {
39 1
    withoutGString({
40 1
      pathname <- Arguments$getReadablePathname(pathname)
41
    })
42
  }
43

44
  # Argument 'default':
45 1
  default <- Arguments$getCharacter(default)
46

47

48
  # Read content
49 1
  bfr <- readLines(pathname, warn=FALSE)
50

51

52
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53
  # Check for knitr-specific commands
54
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55 1
  if (any(regexpr("opts_chunk$set(", bfr, fixed=TRUE) != -1L)) {
56 1
    return("application/x-knitr")
57
  }
58

59
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
60
  # Check for Sweave-specific commands
61
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62 1
  if (any(regexpr("\\SweaveOpts(", bfr, fixed=TRUE) != -1L)) {
63 0
    return("application/x-sweave")
64
  }
65

66 1
  default
67
}) # typeOfRnw()

Read our documentation on viewing source code .

Loading