1

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.

Canada2015
  • 187
  • 1
  • 12
  • 1
    You have the URL for a preview page (in HTML) that contains the image for the PNG file within it. You can see this if you download the file using `download.file(URL, "third_logo.html")`. You'll be able to load this in a browser and view/save the image. There are ways to extract the HTML, but you might find it easier to sync the file to your computer with the Dropbox app and load it from there. – Edward Carney May 19 '19 at 20:17
  • @Edward Carney , Thanks for your good comment. It works now, however, I need the downloaded file in PNG format as I have to use it in the subsequent lines later on. Is it possible to read such a HTML file as PNG in R? – Canada2015 May 19 '19 at 20:39

2 Answers2

2

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')
tasasaki
  • 695
  • 5
  • 12
1

Add ?dl=1 to your URL string. This will download the file itself instead of a preview page. You also need to specify a string for the file name. The file will be saved in your current working directory. You can also save it in a full path. The mode argument is not needed.

URL <- "https://www.dropbox.com/s/cwqr0dxqmgjkna4/third_logo.png?dl=1"
download.file(URL,"third_logo.png")
Edward Carney
  • 1,372
  • 9
  • 7