修改r

问题描述

我构建了一个自定义函数,在其中打印类的输出及其概率(分类器),如下所示:

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