问题描述
我构建了一个自定义函数,在其中打印类的输出及其概率(分类器),如下所示:
fun(formula,data)
> "Class" 0.5
我将此fun
集成到另一个函数new_fun
中,但是我使用了fun
的修改结果作为new_fun
的输出。因此,我不需要原始的fun
类和概率输出。一旦将原始输出集成到new_fun
中,是否有办法避免返回/打印原始输出?
解决方法
您可以使用capture.output
:
f <- function() {print("test"); "result"}
g <- function() { capture.output(result<-f()); result}
f()
#> [1] "test"
#> [1] "result"
g()
#> [1] "result"
由reprex package(v0.3.0)于2020-09-30创建
在您提供的示例中,将是:
new_fun <- function(...){
myformula <- ...
mydata <- ...
#Call to fun with captured output
capture.output( funresult <- fun(myformula,mydata))
#Process further funresult
...
}