在Sweave中沉默包加载消息

时间:2011-11-15 21:25:04

标签: r sweave knitr

我在Sweave文档中加载optmatch,如下所示:

<<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

正如你所看到的,我已经尝试了各种方法来沉默包加载消息,但无济于事。我认为这是因为他们只使用了一个直接的cat()或类似的东西,但它非常令人讨厌。有关如何使这个沉默的想法,以便那些阅读我的最终,美丽,LaTeX ified PDF的人不必阅读RELAX-IV?

其他似乎不起作用的事情(取自Andrie指向related thread的指针):

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

我应该注意到这显然是一个R问题而不是Sweave问题,因为R中也会弹出消息。

2 个答案:

答案 0 :(得分:7)

尝试在隐藏结果块中加载包:

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

如果您使用knitr套餐,则需要quote hide

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

答案 1 :(得分:3)

以下是您的问题的解决方案。包作者使用cat将消息打印到控制台,而不是使用标准消息语句。您可以使用sink将控制台输出转移到临时文件来拦截这些消息:

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

PS。 @XuWang的解决方案仅使用SWeave,因此显然更适合您的情况。