1
###########################################################################/**
2
# @RdocClass RspShSourceCode
3
#
4
# @title "The RspShSourceCode class"
5
#
6
# \description{
7
#  @classhierarchy
8
#
9
#  An RspShSourceCode object is an @see "RspSourceCode" holding shell code.
10
# }
11
#
12
# @synopsis
13
#
14
# \arguments{
15
#   \item{...}{@character strings.}
16
# }
17
#
18
# \section{Fields and Methods}{
19
#  @allmethods
20
# }
21
#
22
# @author
23
#
24
# @keyword internal
25
#*/###########################################################################
26
setConstructorS3("RspShSourceCode", function(...) {
27 2
  extend(RspSourceCode(...), "RspShSourceCode")
28
})
29

30

31

32
#########################################################################/**
33
# @RdocMethod evaluate
34
# @aliasmethod findProcessor
35
#
36
# @title "Evaluates the shell (sh) code"
37
#
38
# \description{
39
#  @get "title".
40
# }
41
#
42
# @synopsis
43
#
44
# \arguments{
45
#   \item{envir}{The @environment in which the RSP string is evaluated.}
46
#   \item{args}{A named @list of arguments assigned to the environment
47
#     in which the RSP string is parsed and evaluated.
48
#     See @see "R.utils::cmdArgs".}
49
#   \item{output}{A @character string specifying how the RSP output
50
#     should be handled/returned.}
51
#   \item{...}{Optional arguments passed to @see "base::eval".}
52
# }
53
#
54
# \value{
55
#  If \code{output="stdout"}, then @NULL is returned and the RSP output
56
#  is sent to the standard output.
57
#  Note that this is output is "buffered", meaning it will be sent to
58
#  standard output upon completion.  This is a limitation of R.
59
#  If \code{output="RspStringProduct"}, then the output is captured
60
#  and returned as an @see "RspStringProduct" with attributes set.
61
# }
62
#
63
# @author
64
#
65
# \seealso{
66
#   @seeclass
67
# }
68
#*/#########################################################################
69
setMethodS3("evaluate", "RspShSourceCode", function(object, envir=parent.frame(), args="*", output=c("RspStringProduct", "stdout"), ..., verbose=FALSE) {
70
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
71
  # Local functions
72
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73 0
  evalSh <- function(text, ...) {
74 0
    pathnameT <- tempfile(pattern="RSP-sh-", fileext=".sh")
75 0
    writeLines(text, con=pathnameT)
76 0
    on.exit({
77 0
      file.remove(pathnameT)
78
    })
79 0
    res <- system2("sh", args=list(pathnameT), stdout=TRUE)
80 0
    res <- paste(res, collapse="\n")
81 0
    res
82 0
  } # evalSh()
83

84

85
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86
  # Validate arguments
87
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88
  # Argument 'args':
89 0
  args <- cmdArgs(args=args)
90

91
  # Argument 'output':
92 0
  output <- match.arg(output)
93

94

95 0
  code <- object
96

97
  # Assign arguments to the parse/evaluation environment
98 0
  attachLocally(args, envir=envir)
99

100
  # Evaluate R source code and capture output
101 0
  res <- evalSh(code)
102

103 0
  if (output == "RspStringProduct") {
104 0
    res <- RspStringProduct(res, type=getType(object))
105
  } else {
106 0
    cat(res)
107 0
    res <- NULL
108
  }
109

110 0
  res
111
}, createGeneric=FALSE) # evaluate()
112

113

114
setMethodS3("findProcessor", "RspShSourceCode", function(object, ...) {
115 0
  function(...) {
116 0
    evaluate(...)
117
  }
118
}) # findProcess()

Read our documentation on viewing source code .

Loading