简体   繁体   中英

Suppress Messages from zip in R

I want to suppress the messages as outputted by the zip command in r but I fail to find the right command to do so.

Background, as I use the zip-function within a function, I don't want the user to see all information about all the files (roughly 5.000, which clutters the console).

Here is what I have tried so far, but all functions foo show either adding: hw.txt (stored 0%) or 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()

Are there any other options to suppress the output of zip ?

The answer will depend on the system that the code is used on. On my Windows system, I can use

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

and it suppresses messages, but it depends on what your system uses to handle zip files. Since the message is coming from the zip program, you must signal it not to output messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM