【问题标题】:Suppressing print() without using sink()在不使用 sink() 的情况下抑制 print()
【发布时间】:2014-03-26 16:50:34
【问题描述】:

我学习了如何检测时间序列中的异常,我使用来自TSA 包的detectAOdetectIO 函数。我在循环中使用它们,但它们的输出没有被抑制。据我所知,当您在循环或函数中使用某些函数时,它们的输出会被抑制,直到您使用 print() 函数。于是查看了这些函数的源码,作者使用了print()。有什么方法可以在不使用sink('/dev/null') 的情况下抑制此输出?

require(TSA)

detected<-numeric(1000)

for(i in 1:10){

  data<-rcauchy(1000)
  model<-auto.arima(data)

  #I don't want this to be printed
  detected<-detectAO(model)$ind

  #Only this
  print(detected)
}

【问题讨论】:

  • 你能发布一些最小的可重现代码吗?

标签: r


【解决方案1】:
require(forecast) ## for auto.arima?                                                                                                                                                                                                          

for(i in 1:10){

  data <- rcauchy(1000)
  model <- auto.arima(data)

  ##I don't want this to be printed                                                                                                                                                                                                           
  capture.output(detected <- detectAO(model)$ind)

  ##Only this                                                                                                                                                                                                                                 
  print(detected)

}

【讨论】:

  • 谢谢,它完美无缺。当然,我忘记了预测。
  • @SebastianOsiński 如果它解决了问题,请随意将其标记为选定答案
猜你喜欢
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多