File: withSink.Rd

package info (click to toggle)
r-cran-r.utils 2.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,864 kB
  • sloc: sh: 18; makefile: 6
file content (74 lines) | stat: -rw-r--r-- 2,116 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Do not modify this file since it was automatically generated from:
% 
%  withSink.R
% 
% by the Rdoc compiler part of the R.oo package.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\name{withSink}
\alias{withSink}


\title{Evaluate an R expression while temporarily diverting output}

\description{
 Evaluate an R expression while temporarily diverting output.
}

\usage{
withSink(expr, file, append=FALSE, type=c("output", "message"), substitute=TRUE,
  envir=parent.frame())
}

\arguments{
  \item{expr}{The R expression to be evaluated.}
  \item{file}{A writable \code{\link[base:connections]{connection}} or a \code{\link[base]{character}} string naming the
   file to write to.}
  \item{append}{If \code{\link[base:logical]{TRUE}}, the diverted output is appended to the file,
   otherwise not.}
  \item{type}{A \code{\link[base]{character}} string specifying whether to divert output
   sent to the standard output or the standard error.
   See \code{\link[base]{sink}}() for details.}
  \item{substitute}{If \code{\link[base:logical]{TRUE}}, argument \code{expr} is
   \code{\link[base]{substitute}()}:ed, otherwise not.}
  \item{envir}{The \code{\link[base]{environment}} in which the expression should be evaluated.}
}

\value{
 Returns the results of the expression evaluated.
}

\details{
  Upon exit (also on errors), this function will close the requested
  "sink".  If additional sinks (of any type) where also opened during
  the evaluation, those will also be closed with a warning.
}

\author{Henrik Bengtsson}

\examples{
# Divert standard output
pathname <- tempfile(fileext=".output.txt")
res <- withSink(file=pathname, {
  print(letters)
})
mcat(readLines(pathname), sep="\n")


# Divert standard error/messages
pathname <- tempfile(fileext=".message.txt")
res <- withSink(file=pathname, type="message", {
  mprint(LETTERS)
})
mcat(readLines(pathname), sep="\n")
}

\seealso{
  Internally, \code{\link[base]{sink}}() is used to divert any output.
}



\keyword{IO}
\keyword{programming}