How to suppress download.file() "trying URL..." message in R?

dhersz

I know the function has an quiet argument, but I'm trying to suppress the message when quiet = FALSE.

This may be weird, but I came across this issue when testing a package I'm writing. I'm using testthat::expect_message() when setting quiet = FALSE, but the function is not actually suppressing the message (it should, and in fact it usually does with "normal" messages).

I tried it with suppressMessages(), but it didn't work as expected:

url <- "https://github.com/ipeaGIT/gtfstools/raw/master/inst/extdata/spo_gtfs.zip"
download.file(url, destfile = tempfile(), quiet = FALSE)
#> trying URL 'https://github.com/ipeaGIT/gtfstools/raw/master/inst/extdata/spo_gtfs.zip'
#> Content type 'application/zip' length 191108 bytes (186 KB)
#> downloaded 186 KB

suppressMessages(download.file(url, destfile = tempfile(), quiet = FALSE))
#> trying URL 'https://github.com/ipeaGIT/gtfstools/raw/master/inst/extdata/spo_gtfs.zip'
#> Content type 'application/zip' length 191108 bytes (186 KB)
#> downloaded 186 KB

Any ideas on how to suppress it, preferably without changing any options? It's not a lifethreatening situation, but it is making me curious.

Joe Roe

suppressMessages() doesn't work because the progress text isn't an R message(), it's the stdout of the the system library that download.file() delegates the actual downloading to (e.g. libcurl, wget or wininet). quiet = TRUE bypasses this by setting the appropriate command line option of that tool.

You can divert stdout from the R console to a file with sink(). Since you don't need it, you can use nullfile() to open a file connection to the platform-dependent null device:

url <- "https://github.com/ipeaGIT/gtfstools/raw/master/inst/extdata/spo_gtfs.zip"

nullcon <- file(nullfile(), open = "wb")
sink(nullcon, type = "message")
download.file(url, destfile = tempfile(), quiet = FALSE)
sink(type = "message")
close(nullcon)

Note that the second-to-last line is very important – it ends the diversion. Without it, all further messages in the R session will be sent to /dev/null.

Also bear in mind the following warnings from ?sink:

Do not use a connection that is open for sink for any other purpose. The software will stop you closing one such inadvertently.

Do not sink the messages stream unless you understand the source code implementing it and hence the pitfalls.

Personally I'd say this method is too risky to use in a package, especially when the quiet = TRUE option is available.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to suppress R startup message?

From Dev

How to suppress File in use alert message

From Dev

Suppress error message in R

From Dev

Suppress error message in R

From Dev

How to suppress warning message in R regarding closing unused connection?

From Dev

How to suppress the warming message by R when using commandArgs()

From Dev

How to suppress error message of a command?

From Dev

How to suppress OpenCV error message

From Dev

Suppress convergence message in nnet multinom function in R

From Dev

How to suppress [no test files] message on go test

From Dev

PostgreSQL - How to suppress query statement message

From Dev

How to suppress certain error message in 'find' command?

From Dev

How to Define Google Endpoints API File Download Message Endpoint

From Dev

How to stream a file download and display a JSF faces message?

From Dev

How to Define Google Endpoints API File Download Message Endpoint

From Dev

How to download file from internet via R

From Dev

How to download file from internet via R

From Dev

Unable to suppress clipboard warning message when closing a file

From Dev

Suppress Message Box R6025 Pure Virtual Function Call

From Java

How to suppress binary file matching results in grep

From Dev

How to suppress Windows file handling with python?

From Dev

How to Suppress Warnings in Sonar for properties file?

From Dev

R download file redirect

From Dev

download csv file in R

From Dev

download csv file in R

From Dev

Error in download file in R

From Dev

How do I suppress the "New release '12.10' available" message?

From Dev

How do I suppress the "New release '12.10' available" message?

From Dev

How to suppress a message from a program while installing it to use in a bash program?

Related Related

  1. 1

    How to suppress R startup message?

  2. 2

    How to suppress File in use alert message

  3. 3

    Suppress error message in R

  4. 4

    Suppress error message in R

  5. 5

    How to suppress warning message in R regarding closing unused connection?

  6. 6

    How to suppress the warming message by R when using commandArgs()

  7. 7

    How to suppress error message of a command?

  8. 8

    How to suppress OpenCV error message

  9. 9

    Suppress convergence message in nnet multinom function in R

  10. 10

    How to suppress [no test files] message on go test

  11. 11

    PostgreSQL - How to suppress query statement message

  12. 12

    How to suppress certain error message in 'find' command?

  13. 13

    How to Define Google Endpoints API File Download Message Endpoint

  14. 14

    How to stream a file download and display a JSF faces message?

  15. 15

    How to Define Google Endpoints API File Download Message Endpoint

  16. 16

    How to download file from internet via R

  17. 17

    How to download file from internet via R

  18. 18

    Unable to suppress clipboard warning message when closing a file

  19. 19

    Suppress Message Box R6025 Pure Virtual Function Call

  20. 20

    How to suppress binary file matching results in grep

  21. 21

    How to suppress Windows file handling with python?

  22. 22

    How to Suppress Warnings in Sonar for properties file?

  23. 23

    R download file redirect

  24. 24

    download csv file in R

  25. 25

    download csv file in R

  26. 26

    Error in download file in R

  27. 27

    How do I suppress the "New release '12.10' available" message?

  28. 28

    How do I suppress the "New release '12.10' available" message?

  29. 29

    How to suppress a message from a program while installing it to use in a bash program?

HotTag

Archive