14

I am trying to download a file in R 3.0.1 (Windows 7):

    fileUrl <- "https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD"
    download.file(fileUrl, destfile="./data/cameras.csv", method="curl")

I checked both the url and my internet connection and they seem to be working just fine. However, I get this message:

    Warning message:  
    In download.file(fileUrl, destfile = "./data/cameras.csv", method = "curl") : 
    download had nonzero exit status

Can't find any help online, anybody knows how to fix this?

lmo
  • 37,904
  • 9
  • 56
  • 69
Renne007
  • 1,087
  • 2
  • 10
  • 13
  • 7
    I don't use Windows but I think the problem is related to `method = "curl"` remove this parameter and try again – dickoa Jun 25 '13 at 14:51
  • Thank you dickoa! I did what you said and this is what I get now: `Error in download.file(fileUrl, destfile = "./data/cameras.csv") : cannot open destfile './data/cameras.csv', reason 'No such file or directory'` – Renne007 Jun 25 '13 at 14:54
  • what is the output of `file.exists("./data")` ? – dickoa Jun 25 '13 at 17:14
  • I proposed a solution but I don't have access to a windows machine to check. Let me know if it works – dickoa Jun 25 '13 at 17:52
  • Do you have write access to the destination folder? Is the disk full? – Hong Ooi Jun 25 '13 at 18:06
  • @dickoa I did the same without adding curl, and it works fine without it. Isn't curl for Mac and not for Windows? – Pj_ Aug 17 '14 at 09:15
  • I am getting curl not found in ubuntu – Arun Raja Nov 09 '15 at 06:29

15 Answers15

17

Still don't understand why removing method = "curl" don't solve the problem.

Another solution is install the downloader package which wrap download.file and make the download process easier and cross-platform (one function with same paramters for all OS)

install.packages("downloader")
fileUrl <- "https://data.baltimorecity.gov/api/views/dz54-2aru
            /rows.csv?accessType=DOWNLOAD"

require(downloader)
download(fileUrl, "data/cameras.csv", mode = "wb")

Hope that it will work this time

dickoa
  • 18,217
  • 3
  • 36
  • 50
10

The answer by @dickoa probably works, but I think the major issue is that you are using https unnecessarily. I think this works:

# Note the http instead of https
file<-'http://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD'
read.csv(file)
nograpes
  • 18,623
  • 1
  • 44
  • 67
  • 3
    -1 because I think this answer does not gives a solution to when `https` is the only protocol available. It is a pure coincidence that the above CSV file is served on both `http` and `https` but it is not always the case and this answer does not generalise. – Giuseppe Romagnuolo Feb 05 '15 at 21:57
  • @GiuseppeR You're right. dickoa's answer is certainly better. I also don't understand why removing `method = 'curl'` didn't work for Renne007. Probably some weird proxy thing. – nograpes Feb 05 '15 at 23:05
4

Try another method: ..., method = "libcurl")

Denis Rasulev
  • 3,744
  • 4
  • 33
  • 47
  • 2
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – eirikir Sep 26 '15 at 16:31
  • Sorry, but this does provide an answer to question! Question was "how to fix it?", so the answer is "try to use another method. If the author of the question will use "libcurl" instead of "curl" he won't get warning message. – Denis Rasulev Sep 26 '15 at 16:35
  • 1
    Here is the proof: "R 3.2 includes two new download methods (“libcurl” and “wininet”) that both support HTTPS connections. We recommend that you use these new methods when running under R 3.2." Read more here: https://support.rstudio.com/hc/en-us/articles/206827897-Secure-Package-Downloads-for-R – Denis Rasulev Sep 27 '15 at 01:53
3
file<-'http://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD'
download.file(file, destfile="cameras.csv")
Ryan M
  • 18,333
  • 31
  • 67
  • 74
ronald
  • 41
  • 1
3

Use setInternet2 before you download. it worked for me.

setInternet2(use=T)

file <- "URL"

download.file(file,destfile= "./data/cameras.csv")

2

fileUrl1 <- "https://... xyz.csv"

download.file(fileUrl1, destfile="./data/xyz.csv", method="curl")

Use the following (http instead of https in the 1st line and in 2nd line remove mehod="curl")

fileUrl1 <- "http://... xyz.csv"
download.file(fileUrl1, destfile="./data/xyz.csv")

try this ,you can download the csv file

Community
  • 1
  • 1
2

@dickoa: Given the fact that you get "TRUE" when you do:

file.exists("./data")

I suppose you have written initially:

if (!file.exists("data")) { file.create("data") }

before you perform the code you shared to download the csv. However, this creates a file "data", not a directory. So the following should be written in your R script before your code:

if (!file.exists("data")) { dir.create("data") }

After this, I believe your code should work fine. Hope this helps.

Diogo A.
  • 21
  • 1
1

When you call download.file(), it doesn't create the directory for you.
Instead you need to have a valid directory already created for it to create the file.
My guess is that you haven't created the folder called data yet.

Hope this helps.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
SalicBlu3
  • 1,874
  • 2
  • 19
  • 32
1

I found this works after by omitting the ssl in https ie

NatGas <- "http://d396qusza40orc.cloudfront.net/getdata%2Fdata%2FDATA.gov_NGAP.xlsx"

and then set the method to "auto"

download.file(NatGas, destfile = "./TidyData/NatGas.xlsx",method = "auto", mode ="wb")

R.Warner
  • 11
  • 2
1

It just happened to me. In order to get it working, you just need to get rid of the 's' in 'https' and also don't specify 'method = curl'. I'm just beginning so got no idea how it works, but hey it does the job.

Anderson
  • 531
  • 1
  • 5
  • 8
0

Try changing the line from "method=curl" to "method=internal"

If you want to use the curl method, you need to install the curl library onto your computer, at http://curl.haxx.se/

thy
  • 1
0

I tried two methods for downloading the same file:

  1. Download the package "downloader" using install.packages("downloader") and then load the package using require(downloader) command. After this, use the command: download(fileurl,"./data/camera.csv",mode="wb")

  2. The other method is:

    file<-'http://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD' read.csv(file)

and save the data.frame file using write.csv method to save the file.

Karthick Kumar
  • 2,349
  • 1
  • 17
  • 30
0

I found that I had to actually download curl from the haxx site and then add its location to the path in the system environment variables. After that the

download.file(fileURL, destfile = "data/cameras.csv", method = "curl") 

command worked fine. This was on a Windows 7, 64bit machine.

0

if you write method="libcurl" then it will work for both http or https url

ajay
  • 43
  • 6
0

I have gotten the same error a sec ago the exact same task at hand. For me the issue was, that I was not in the right working directory.

The solution: in the right bottom corner window (compare picture), click "More" and than "Set as working directory".

enter image description here

Try the download again! For me it worked fine.

ChrisDelClea
  • 307
  • 2
  • 8