2011-05-30 119 views
5

我玩一个位与LowRankQP()包中的R,甚至设置verbose=FALSE仍然产生大量的输出(见下例)。如何沉默R包的输出?

输出来自编译部分的代码。有没有一种方法(一个包装功能部件?)中的R拨打电话到LowRankQP()绝对沉默(即不打印屏幕上的任何东西),而无需修改底层的编译代码(既不与此包关联的电子邮件地址是仍然活跃)?

library(LowRankQP) 

Vmat <- matrix(0,6,6) 
diag(Vmat) <- c(1, 1,1,0,0,0) 
dvec <- c(0,-5,0,0,0,0) 
Amat <- matrix(c(-4,-3,0,-1,0,0,2,1,0,0,-1,0,0,-2,1,0,0,-1),6,3) 
bvec <- c(-8,2,0) 
uvec <- c(100,100,100,100,100,100) 

aa<-LowRankQP(Vmat,dvec,t(Amat),bvec,uvec,method="CHOL") 

# LowRankQP CONVERGED IN 15 ITERATIONS 
# 
#  Primal Feasibility = 2.5719308e-16 
#  Dual Feasibility  = 7.1949984e-16 
#  Complementarity Value = 3.3066705e-11 
#  Duality Gap   = 3.3065273e-11 
#  Termination Condition = 9.7802929e-12 

这是一个以 “LowRankQP会聚到15次迭代”,我想逃脱..

的Ubuntu 11.04,R版本2.12.1和LowRankQP()1.0.1开始部分。

+1

请添加一个可重复的例子。我从我做过的几个测试中没有遇到任何问题。 – 2011-05-30 14:49:52

+2

在Windows上不可重现,我无法获得任何输出。请1)更新到最新版本,2)如果这种情况持续存在,请通知软件包的开发者。这听起来像Linux和Windows版本之间的一些错误/不一致。 – 2011-05-30 16:10:58

+0

谢谢J.M.(我已经运行R /包的最新版本,所以我会邮寄devel)。 – user189035 2011-05-30 17:59:25

回答

5

接收器(文件= NULL)不起作用,因为它关闭最后的接收器,仅此而已。

片(文件= NULL)警告消息:在水槽(文件= NULL):没有下陷删除

什么工作,虽然是:

f = file() 
sink(file=f) ## silence upcoming output using anonymous file connection 
... your code here ... 
sink() ## undo silencing 
close(f) 

使用匿名文件具有与平台无关的优点,即您不必创建临时文件名。

实施例:

f = file() 
sink(file=f) 
example(glm) 
sink() 
close(f) 

我用水槽()成功用于其它功能(例如normalmixEM2comp {mixtools})。

(编辑: - 如果你拨打上面的代码往往不够,甚至是错误这篇文章的第一个版本没有使用明确的文件句柄,从而给了一个警告)。这现在通过使用close(f)来解决。

0

我不它是否会工作,但你可以尝试invisible()。我不知道你正在使用的功能,我不知道如果看不见,可以沉默cat()。但你可以放弃。

+0

恩:感谢提示,但它不起作用 – user189035 2011-05-30 15:06:22

+3

所有看不见的确是,是防止其被打印内容的返回值,所以这应该不会有帮助。 – 2011-05-31 06:51:43

2

你熟悉R函数sink()?从其帮助页面:

sink     package:base     R Documentation 

Send R Output to a File 

Description: 

    ‘sink’ diverts R output to a connection. 

    ‘sink.number()’ reports how many diversions are in use. 

    ‘sink.number(type = "message")’ reports the number of the 
    connection currently being used for error messages. 

Usage: 

    sink(file = NULL, append = FALSE, type = c("output", "message"), 
      split = FALSE) 

    sink.number(type = c("output", "message")) 

Arguments: 

    file: a writable connection or a character string naming the file 
      to write to, or ‘NULL’ to stop sink-ing. 

file=NULL参数是你可能想要在这里。

+0

谢谢D.E.你知道有什么方法可以避免这些打印输出出现在终端窗口 - 这很烦人吗? – user189035 2011-05-30 16:41:19

+0

'sink(file = NULL)'这样做:** all **输出变为NULL。 – 2011-05-30 18:07:20

+0

谢谢D.E .:然后我一直无法使用它:在上面的代码中,我应该沉到哪里(file = NULL),以便消息不会在屏幕上打印? – user189035 2011-05-30 19:20:13

1

好的,我可以通过注释线413-423> 418 LowRankQP.c的和重新安装,从名为.tar.gz(或可替代地添加新选项详细== 2)做。