[R] Sweave: Incorporating warnings into a Sweave output chunck

Duncan Mackay dulcalma at bigpond.com
Wed Jan 4 23:22:41 CET 2017

Hi Thierry


Thank you for your comments.

I had considered it in the past but decided against it. 

May think again about switching but would have to change a lot of macros in my text editor.







From: Thierry Onkelinx [mailto:thierry.onkelinx at inbo.be] 
Sent: Thursday, 5 January 2017 07:06
To: Duncan Mackay
Cc: R
Subject: Re: [R] Sweave: Incorporating warnings into a Sweave output chunck


Dear Duncan,


I'd recommend to switch from Sweave to knitr. Knitr has more options for handling warnings and errors than Sweave.


Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest 
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance 
Kliniekstraat 25
1070 Anderlecht

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner 
The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey


2017-01-04 4:07 GMT+01:00 Duncan Mackay <dulcalma at bigpond.com>:

Dear All

There are some occasions I have run code in R without warning.
After incorporating the code into an .Rnw file and running by Sweave I find
there are  warnings sent to the screen.

On some occasions when I use Sweave(...)  I would like to incorporate the
warnings into the resulting tex file.
Until now I have done it manually which is a bit of a pain.

Does anyone know of a way?  It would be nice to have a Sweave option to
include it.

The only reference I can find is


which had no replies except this one on Nabble


I have got the code from the attached Rnw file working in R version 3.3.1
But there are some problems with it.

Below is  the code including some of the options and setup with the function
in one chunck

<<redefwarning, echo=FALSE>>=
  options(warn = 1)
  cons <- showConnections(all = TRUE)
 # .CurFileName <- get("file", env = parent.frame(3)) # modified by next
  .CurFileName <-
  subset(data.frame(cons), class == "file" & nchar(description) > 0)[1]) )
  .PrefixName <- strsplit(.CurFileName, "\\. <file:///\\.> ")[[1]][1]
  .LatexFileName <- paste(.PrefixName, "tex", sepo = ".")
  .LatexFileCon <-
  getConnection(what = as.integer(rownames(cons)[which(cons[,1] ==
  sink(file = .LatexFileCon, append = TRUE, type = "message")

warningbck <- warning
warning <-
function (..., call. = TRUE, immediate. = FALSE, domain = NULL){

  args <- list(...)

  if (length(args) == 1 && inherits(args[[1]], "condition")) {

    cond <- args[[1]]
    message <- conditionMessage(cond)
    call <- conditionCall(cond)

      .Internal(.signalCondition(cond, message, call))
      .Internal(.dfltStop(message, call))
    muffleWarning = function() NULL)


  }  else {

    if (length(args) > 0) {

      args <- lapply(list(...), as.character)

      if (is.null(domain) || !is.na(domain))
        args <- .Internal(gettext(domain, unlist(args)))
        message <- paste(args, collapse = "")

    } else{

      message <- ""

    writeLines(text = "\n\\end{Sinput}\n\\begin{Soutput}", con =
    .Internal(warning(as.logical(call.), as.logical(immediate.), message))

    writeLines(text = "\\end{Soutput}\n\\begin{Sinput} <file:///\\end%7bSoutput%7d\n\begin%7bSinput%7d> ", con =

This puts the warning into the input chunck directly after the R command
e.g. as shown below

  clust.hw <- svydesign(ids = ~Patient, data = hw.dat)Warning in
svydesign.default(ids = ~Patient, data = hwd) :
  No weights or probabilities supplied, assuming equal probability


I think that there also needs to be an argument about closing connections
somewhere in the code.



Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]

More information about the R-help mailing list