1
###########################################################################/**
2
# @RdocClass RspCode
3
#
4
# @title "The RspCode class"
5
#
6
# \description{
7
#  @classhierarchy
8
#
9
#  An RspCode is an @see "RspExpression" that represents a piece of source
10
#  code, which may or may not be a complete code chunk (expression).
11
# }
12
#
13
# @synopsis
14
#
15
# \arguments{
16
#   \item{code}{A @character string.}
17
#   \item{echo}{If @TRUE, code is echoed to the output.}
18
#   \item{...}{Not used.}
19
# }
20
#
21
# \section{Fields and Methods}{
22
#  @allmethods
23
# }
24
#
25
# @author
26
#
27
# @keyword internal
28
#*/###########################################################################
29
setConstructorS3("RspCode", function(code=character(), echo=FALSE, ...) {
30
  # Replace all '\r\n' and '\r' with '\n' newlines
31 2
  code <- gsub("\r\n", "\n", code)
32 2
  code <- gsub("\r", "\n", code)
33

34 2
  extend(RspExpression(code, echo=echo, ...), "RspCode")
35
})
36

37

38
#########################################################################/**
39
# @RdocMethod getCode
40
#
41
# @title "Gets the source code"
42
#
43
# \description{
44
#  @get "title".
45
# }
46
#
47
# @synopsis
48
#
49
# \arguments{
50
#   \item{...}{Not used.}
51
# }
52
#
53
# \value{
54
#  Returns a @character string.
55
# }
56
#
57
# @author
58
#
59
# \seealso{
60
#   @seeclass
61
# }
62
#*/#########################################################################
63
setMethodS3("getCode", "RspCode", function(code, ...) {
64 2
  as.character(code)
65
})
66

67

68
#########################################################################/**
69
# @RdocMethod getEcho
70
#
71
# @title "Checks whether the source code should be echoed or not"
72
#
73
# \description{
74
#  @get "title".
75
# }
76
#
77
# @synopsis
78
#
79
# \arguments{
80
#   \item{...}{Not used.}
81
# }
82
#
83
# \value{
84
#  Returns a @logical.
85
# }
86
#
87
# @author
88
#
89
# \seealso{
90
#   @seeclass
91
# }
92
#*/#########################################################################
93
setMethodS3("getEcho", "RspCode", function(code, ...) {
94 2
  isTRUE(getAttribute(code, "echo", default=FALSE))
95
})
96

97

98
setMethodS3("asRspString", "RspCode", function(code, ...) {
99 2
  body <- getCode(code)
100

101 2
  if (getEcho(code)) {
102 0
    fmtstr <- ":%s"
103
  } else {
104 2
    fmtstr <- "%s"
105
  }
106

107 2
  fmtstr <- paste(escFmtStr(.rspBracketOpen), fmtstr, escFmtStr(.rspBracketClose), sep="")
108 2
  s <- sprintf(fmtstr, body)
109 2
  RspString(s)
110
})
111

112

113

114

115
###########################################################################/**
116
# @RdocClass RspCodeChunk
117
#
118
# @title "The RspCodeChunk class"
119
#
120
# \description{
121
#  @classhierarchy
122
#
123
#  An RspCodeChunk is an @see "RspCode" that represents a complete
124
#  RSP code chunk.
125
# }
126
#
127
# @synopsis
128
#
129
# \arguments{
130
#   \item{...}{Arguments passed to the constructor of @see "RspCode".}
131
#   \item{return}{If @TRUE, the value of the evaluated code chunk is returned.}
132
# }
133
#
134
# \section{Fields and Methods}{
135
#  @allmethods
136
# }
137
#
138
# @author
139
#
140
# @keyword internal
141
#*/###########################################################################
142
setConstructorS3("RspCodeChunk", function(..., return=FALSE) {
143 2
  extend(RspCode(..., return=return), "RspCodeChunk")
144
})
145

146

147
setMethodS3("getInclude", "RspCodeChunk", function(code, ...) {
148 2
  isTRUE(getAttribute(code, "return", default=FALSE))
149
})
150

151

152
setMethodS3("asRspString", "RspCodeChunk", function(code, ...) {
153 2
  body <- getCode(code)
154

155 2
  if (getEcho(code)) {
156 0
    fmtstr <- ":%s"
157 2
  } else if (getInclude(code)) {
158 2
    fmtstr <- "=%s"
159
  } else {
160 2
    fmtstr <- "%s"
161
  }
162

163 2
  fmtstr <- paste(escFmtStr(.rspBracketOpen), fmtstr, escFmtStr(.rspBracketClose), sep="")
164 2
  s <- sprintf(fmtstr, body)
165 2
  RspString(s)
166
})

Read our documentation on viewing source code .

Loading