1
###########################################################################/**
2
# @RdocDefault rclean
3
# @alias rclean.RspString
4
# @alias rclean.RspDocument
5
#
6
# @title "Compiles an RSP document into a preprocessed and validated RSP document"
7
#
8
# \description{
9
#  @get "title".
10
# }
11
#
12
# @synopsis
13
#
14
# \arguments{
15
#   \item{...}{@character strings with RSP markup.}
16
#   \item{file, path}{Alternatively, a file, a URL or a @connection from
17
#      with the strings are read.
18
#      If a file, the \code{path} is prepended to the file, iff given.}
19
#   \item{envir}{The @environment in which the RSP string is preprocessed.}
20
#   \item{args}{A named @list of arguments assigned to the environment
21
#     in which the RSP document is parsed.
22
#     See @see "R.utils::cmdArgs".}
23
#   \item{verbose}{See @see "R.utils::Verbose".}
24
# }
25
#
26
# \value{
27
#   Returns an @see "RspString".
28
# }
29
#
30
# @examples "../incl/rclean.Rex"
31
#
32
# @author
33
#
34
# \seealso{
35
#  @see "rcat" and @see "rfile".
36
# }
37
#
38
# @keyword file
39
# @keyword IO
40
# @keyword internal
41
#*/###########################################################################
42
setMethodS3("rclean", "default", function(..., file=NULL, path=NULL, envir=parent.frame(), args="*", verbose=FALSE) {
43
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44
  # Validate arguments
45
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46
  # Argument 'file' & 'path':
47 1
  if (inherits(file, "connection")) {
48 1
  } else if (is.character(file)) {
49 0
    if (!is.null(path)) {
50 0
      file <- file.path(path, file)
51
    }
52 0
    if (!isUrl(file)) {
53 0
      file <- Arguments$getReadablePathname(file, absolute=TRUE)
54
    }
55
  }
56

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

64

65 1
  verbose && enter(verbose, "rclean() for default")
66

67 1
  if (is.null(file)) {
68 1
    s <- RspString(...)
69
  } else {
70 0
    verbose && cat(verbose, "Input file: ", file)
71 0
    s <- .readText(file)
72 0
    s <- RspString(s, source=file, ...)
73 0
    s <- setMetadata(s, name="source", value=file)
74
  }
75 1
  verbose && cat(verbose, "Length of RSP string: ", nchar(s))
76

77 1
  res <- rclean(s, envir=envir, args=args, verbose=verbose)
78

79 1
  verbose && exit(verbose)
80

81 1
  res
82
}) # rclean()
83

84

85
setMethodS3("rclean", "RspString", function(object, envir=parent.frame(), args="*", ..., verbose=FALSE) {
86
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
87
  # Validate arguments
88
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
89
  # Argument 'args':
90 1
  args <- cmdArgs(args=args)
91

92
  # Argument 'verbose':
93 1
  verbose <- Arguments$getVerbose(verbose)
94 1
  if (verbose) {
95 1
    pushState(verbose)
96 1
    on.exit(popState(verbose))
97
  }
98

99 1
  verbose && enter(verbose, "rclean() for ", class(object)[1L])
100

101 1
  if (length(args) > 0L) {
102 0
    verbose && enter(verbose, "Assigning RSP arguments to processing environment")
103 0
    verbose && cat(verbose, "Environment: ", getName(envir))
104

105 0
    verbose && cat(verbose, "RSP arguments:")
106 0
    verbose && str(verbose, args)
107

108
    # Assign arguments to the parse/evaluation environment
109 0
    names <- attachLocally(args, envir=envir)
110 0
    if (verbose) {
111 0
      if (length(names) > 0L) {
112 0
        printf(verbose, "Variables assigned: [%d] %s\n", length(names), hpaste(names))
113 0
        member <- NULL; rm(list="member"); # To please R CMD check
114 0
        ll <- subset(ll(envir=envir), member %in% names)
115 0
        print(verbose, ll)
116
      }
117
    }
118 0
    verbose && exit(verbose)
119
  } else {
120 1
    names <- NULL
121
  }
122

123 1
  if (verbose) {
124 1
    enter(verbose, "Parse RSP string to RSP document")
125 1
    cat(verbose, "Parse environment: ", getName(envir))
126 1
    if (length(names) > 0L) {
127 0
      ll <- subset(ll(envir=envir), member %in% names)
128 0
      print(verbose, ll)
129
    }
130
  }
131 1
  doc <- parseDocument(object, envir=envir, ..., verbose=verbose)
132 1
  verbose && print(verbose, doc)
133 1
  verbose && exit(verbose)
134

135 1
  res <- rclean(doc, envir=envir, args=NULL, ..., verbose=verbose)
136

137 1
  verbose && exit(verbose)
138

139 1
  res
140
}) # rclean()
141

142

143
setMethodS3("rclean", "RspDocument", function(object, envir=parent.frame(), ..., verbose=FALSE) {
144
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
145
  # Validate arguments
146
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
147
  # Argument 'verbose':
148 1
  verbose <- Arguments$getVerbose(verbose)
149 1
  if (verbose) {
150 1
    pushState(verbose)
151 1
    on.exit(popState(verbose))
152
  }
153

154 1
  verbose && enter(verbose, "rclean() for ", class(object)[1L])
155

156 1
  verbose && enter(verbose, "Coerce RSP document to RSP string")
157 1
  s <- asRspString(object)
158 1
  verbose && exit(verbose)
159

160 1
  verbose && exit(verbose)
161

162 1
  s
163
}) # rclean()

Read our documentation on viewing source code .

Loading