Silencing a package load message in Sweave

2019-02-17 11:35发布

问题:

I'm loading optmatch in a Sweave document as follows:

<<myCodeBlock, echo=FALSE>>=
library(optmatch, quietly=TRUE)
@

You're loading optmatch, by Ben Hansen, a package for flexible
and optimal matching. Important license information:
The optmatch package makes essential use of D. P. Bertsekas
and P. Tseng's RELAX-IV algorithm and code, as well as
Bertsekas' AUCTION algorithm and code.
Bertsekas and Tseng freely permit their software to be used for
research purposes, but non-research uses, including the use of it
to 'satisfy in any part commercial delivery requirements to
government or industry,' require a special agreement with them.
By extension, this requirement applies to any use of the
fullmatch() function. (If you are using another package that has
loaded optmatch, then you will probably be using fullmatch indirectly.)
For more information, enter relaxinfo() at the command line

As you can see, I've tried every way I can think of to silence the package load message, to no avail. I assume this is because they just used a straight-up cat() or something like that, but it's mighty annoying. Any thoughts on how to silence this so that those reading my final, beautiful, LaTeXified PDF don't have to read about RELAX-IV?

Other things that don't seem to work (take from Andrie's pointer to a related thread):

suppressMessages(library(optmatch))
suppressPackageStartupMessages(require("optmatch"))

I should note this is pretty obviously an R problem not a Sweave problem, as the messages pop up in R also.

回答1:

Try loading the package in a hide results chunk:

<<packages,results=hide>>= 
require(optmatch) 
@

If you use the knitr package, you need to quote hide:

<<packages,results='hide'>>= 
require(optmatch) 
@


回答2:

Here is an R solution to your problem. The package author uses cat to print the messages to the console, rather than using standard message statements. You can intercept these messages by using sink to divert console output to a temporary file:

<<myCodeBlock, echo=FALSE>>=
zz <- tempfile()
sink(file=zz)
library(optmatch, quietly=TRUE))
unlink(zz)
@

PS. The solution by @XuWang uses only SWeave, so is clearly much more suitable in your case.



标签: r sweave knitr