我正在玩R中的LowRankQP()
包,甚至设置verbose=FALSE
仍会产生大量输出(参见下面的示例)。
输出来自代码的编译部分。 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。
答案 0 :(得分:6)
sink(file = NULL)不起作用,因为它关闭最后一个接收器,仅此而已。
sink(file = NULL)警告消息:在接收器中(file = 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)
我已成功将sink()用于其他功能(例如normalmixEM2comp {mixtools})。
(编辑:这篇文章的第一个版本没有使用显式文件句柄,因此发出警告 - 如果你经常调用上面的代码片段,甚至会出错)。现在通过使用close(f)修复此问题。
答案 1 :(得分: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
参数。
答案 2 :(得分:1)
好的,我可以通过评论LowRankQP.c的第413-> 418行并从.tar.gz重新安装它(或者添加新选项verbose == 2)来实现。
答案 3 :(得分:1)
根据cbielow的回答,这里有一个小功能,可以使For VarDeleteLoop = NumRows To 2 Step -1
和cat()
(但不是print()
或message()
)保持沉默并返回任何内容表达式返回:
warning()
试验:
shut_up = function(expr) {
#temp file
f = file()
#write output to that file
sink(file = f)
#evaluate expr in original environment
y = eval(expr, envir = parent.frame())
#close sink
sink()
#get rid of file
close(f)
y
}
请注意,> shut_up(print(1))
[1] 1
> shut_up(cat(1))
NULL
> shut_up(message(1))
1
NULL
> shut_up(warning(1))
[1] "1"
Warning message:
In eval(expr, envir = parent.frame()) : 1
和print()
会返回输出,因为这些函数会以静默方式返回其输入,而warning()
和cat()
会返回message()
。
答案 4 :(得分:0)
我不会,如果它可以工作,但你可以尝试invisible()
。我不知道你正在使用的功能,我不知道隐形是否可以沉默cat()
。但你可以放手一搏。