Search code examples
rdownloadpngmode

R: Why downloaded PNG file using download.file() can not be opened?


I had a question, but could not find my answer is the following links:

Download png/jpg with R

Downloading png from Shiny (R)

Use href and target in download.file R?

What's the "internal method" of R's download.file?

Question:

I am using this code to download a PNG file. However, the downloaded PNG file is not readable (windows photo viewer can't open this picture doesn't support this file format).

Please note: The original PNG file is readable easily in both R and Windows.

It seems the problem comes from mode=" " option in download.file()

I tried these three modes there, but still the problem is there:

mode = "wb"
mode = "w"
mode = "ab"

Here is my code:

URL <- "https://www.dropbox.com/s/cwqr0dxqmgjkna4/third_logo.png"

download.file(URL, mode = 'wb')

Any thought would be highly Appreciated.


Solution

  • Dropbox provides the way to download contents directly.
    https://zapier.com/learn/how-to/generate-direct-dropbox-link/

    Your code should be

    URL <- "https://dl.dropboxusercontent.com/s/cwqr0dxqmgjkna4/third_logo.png"
    download.file(URL, destfile = "test.png", mode = 'wb')