Search code examples
rdownloadcharacter-encodingnon-ascii-characters

R. download.file using link with special characters (ñ) - Linux vs Windows


I need to download the zip file contained in an URL. If you just copy and paste works perfectly in the browser. The following code was used in linux, and it worked perfectly as well.

link <- "http://www.catastro.minhap.es/INSPIRE/Buildings/32/32035-A GUDIÑA/A.ES.SDGC.BU.32035.zip"
tempzip <- "./temp/"
download.file(link, destfile = tempzip, method = "auto")

Now I'm looking to download the file as well in windows, but it doesn't work. Here is the error in Windows:

> download.file(link, destfile = tempzip, method = "auto")
trying URL 'http://www.catastro.minhap.es/INSPIRE/Buildings/32/32035-A GUDIÑA/A.ES.SDGC.BU.32035.zip'
Error in download.file(ziplink, destfile = tempzip, method = "auto") : 
  cannot open URL 'http://www.catastro.minhap.es/INSPIRE/Buildings/32/32035-A GUDIÑA/A.ES.SDGC.BU.32035.zip'
In addition: Warning message:
In download.file(ziplink, destfile = tempzip, method = "auto") :
  cannot open URL 'http://www.catastro.minhap.es/INSPIRE/Buildings/32/32035-A GUDIÑA/A.ES.SDGC.BU.32035.zip': HTTP status was '404 Not Found'

How can I use a function that works both in windows and linux using the same link ?


Solution

  • You can use URLencode to convert to a url-escaped string that should be platform-independent. The following result was obtained on a Windows PC:

    download.file(URLencode(link), destfile = "myfile.zip", method = "auto")
    #> trying URL 'http://www.catastro.minhap.es/INSPIRE/Buildings/32/32035-
    #> A%20GUDI%D1A/A.ES.SDGC.BU.32035.zip'
    #> Content type 'application/x-zip-compressed' length 706283 bytes (689 KB)
    #> downloaded 689 KB