【问题标题】:Suppress Messages from zip in R在 R 中抑制来自 zip 的消息
【发布时间】:2018-01-28 14:19:10
【问题描述】:

我想在r 中抑制zip 命令输出的消息,但我找不到执行此操作的正确命令。

背景,当我在函数中使用 zip 函数时,我不希望用户看到所有文件的所有信息(大约 5.000,这会使控制台混乱)。

这是我迄今为止尝试过的,但所有函数 foo 都显示 adding: hw.txt (stored 0%)updating: hw.txt (stored 0%)

# create a small file 
writeLines("hello world", "hw.txt")
# use the original command
zip("zip.zip", "hw.txt")

# try different options of capturing/suppressing output!

# assignment
foo1 <- function() a <- zip("zip.zip", "hw.txt")
foo1()

# capture.output
foo2 <- function() a <- capture.output(zip("zip.zip", "hw.txt"))
foo2()

# suppressMessages
foo3 <- function() suppressMessages(zip("zip.zip", "hw.txt"))
foo3()

# invisible
foo4 <- function() invisible(zip("zip.zip", "hw.txt"))
foo4()

# sink
foo5 <- function() {
 sink(tempfile())
 zip("zip.zip", "hw.txt")
 sink()
}
foo5()

还有其他选项可以抑制zip 的输出吗?

【问题讨论】:

  • 使用的语言是什么?
  • 我使用 R(我更新了问题以使其更明显)
  • 确实有,这就是我找到上面展示的 5 种方法的地方。但它们似乎不适用于zip

标签: r zip output


【解决方案1】:

答案取决于使用代码的系统。在我的 Windows 系统上,我可以使用

zip("zip.zip", "hw.txt", flags="-q")

它会抑制消息,但这取决于您的系统使用什么来处理 zip 文件。由于消息来自 zip 程序,因此您必须向它发出信号,不要输出消息。

【讨论】:

  • 太棒了,完成了这项工作!我使用 Ubuntu,它就像一个魅力
猜你喜欢
  • 2015-08-10
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 2015-04-04
相关资源
最近更新 更多