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

Thierry Onkelinx thierry.onkelinx at inbo.be
Wed Jan 4 21:06:20 CET 2017


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
Belgium

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 op 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
>
> https://stat.ethz.ch/pipermail/r-help/2006-December/121892.html
>
> which had no replies except this one on Nabble
>
> http://r.789695.n4.nabble.com/R-Sweave-and-warning-messages-td814182.html
>
> 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
> line
>   .CurFileName <-
>   as.vector(unlist(
>   subset(data.frame(cons), class == "file" & nchar(description) > 0)[1]) )
>   .PrefixName <- strsplit(.CurFileName, "\\.")[[1]][1]
>   .LatexFileName <- paste(.PrefixName, "tex", sepo = ".")
>   .LatexFileCon <-
>   getConnection(what = as.integer(rownames(cons)[which(cons[,1] ==
> .LatexFileName)]))
>   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)
>
>     withRestarts({
>       .Internal(.signalCondition(cond, message, call))
>       .Internal(.dfltStop(message, call))
>     },
>     muffleWarning = function() NULL)
>
>     invisible(message)
>
>   }  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 =
> .LatexFileCon)
>     .Internal(warning(as.logical(call.), as.logical(immediate.), message))
>
>     writeLines(text = "\\end{Soutput}\n\\begin{Sinput}", con =
> .LatexFileCon)
>   }
> }
>
> This puts the warning into the input chunck directly after the R command
> e.g. as shown below
>
> \begin{Sinput}
>   clust.hw <- svydesign(ids = ~Patient, data = hw.dat)Warning in
> svydesign.default(ids = ~Patient, data = hwd) :
>   No weights or probabilities supplied, assuming equal probability
>
> \end{Sinput}
>
> I think that there also needs to be an argument about closing connections
> somewhere in the code.
>
>  Regards
>
> Duncan
>
> Duncan Mackay
> Department of Agronomy and Soil Science
> University of New England
> Armidale NSW 2351
> Email: home: mackay op northnet.com.au
>
> ______________________________________________
> R-help op r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> 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