0

我尝试根据函数参数重定向 cat() 输出,如下所示。我不确定如何通过递归引用或堆栈溢出来避免溢出。

testme1 = function(cat=cat) {
cat("This is testme1\n")
}

testme2 = function(cat=function(...){cat(...)}) {
cat("This is testme2\n")
}

> testme1()                           # Error in testme1() : 
                                      # promise evaluation     läuft bereits: rekursive Referenz auf das Standardargument oder frühere Fehler?
> testme1(cat=cat)                    # works
> testme1(cat=function(...)cat(file="huhu.txt",...)) # works

> testme2()                           # Error: protect(): protection stack overflow
> testme2(cat=cat)                    # works
> testme2(cat=function(...)cat(file="huhu.txt",...)) # works
4

1 回答 1

0

您是否考虑过使用 capture.output() ?例如

testme = function() {
  cat("This is testme\n")
}
testme()

output <- capture.output(testme())
output
于 2013-10-24T08:31:56.097 回答