Avoid console message form package function

ぃ、小莉子 提交于 2019-12-23 18:36:21

问题


I'm using a package function (corenv, from seewave) which create a "please wait..." message in console. As I call it iteratively, that message is very annoying. So, I need a way for:

  • From my code, to temporarily ban the console messages

OR

  • To access the function code and take off the message line

The following is not my real code, but a very simple one showing the problem

require(seewave)
a = seq(0, (2*pi), by=0.01) #simple, unreal example
for (i in sequence(100)){
  x = sin(a*i/3) #simple, unreal example
  y = sin(a*i/2) #simple, unreal example
  corenv(x,y,10,plot=FALSE)
}

A very simple question, but I haven't found any solution. I'll aprecciate any help


回答1:


You could use sink to capture the output, e.g.

sink("tmp.txt")
z = corenv(x,y,10,plot=FALSE)
sink()

You can also wrap it in a function, e.g.

## unlink deletes the temporary file
## on.exit ensures the sink is closed even if 
## corenv raises an error.
corenv(..., verbose=FALSE) {
  if(verbose) {
    sink("tmp.txt")
    on.exit(sink(); unlink("tmp.txt"))
  }
  seewave::corenv(...)
}


来源:https://stackoverflow.com/questions/39154353/avoid-console-message-form-package-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!