3

I am trying to download and read a zipped csv file from Kaggle within an R script. After researching other posts including post1 and post2 I have tried:

# Read data with temp file
url <- "https://www.kaggle.com/c/rossmann-store-sales/download/store.csv.zip"
tmp <- tempfile()
download.file(url, tmp, mode = "wb")
con <- unz(tmp, "store.csv.zip")
store <- read.table(con, sep = ",", header = TRUE)
unlink(tmp)

the read.table command throws an error:

Error in open.connection(file, "rt") : cannot open the connection

I have also tried:

# Download file, unzip, and read
url <- "https://www.kaggle.com/c/rossmann-store-sales/download/store.csv.zip"
download.file(url, destfile = "./SourceData/store.csv.zip", mode = "wb")
unzip("./SourceData/store.csv.zip")

Unzip throws the error:

error 1 in extracting from zip file

Bypassing the unzip command and reading directly from the zip file

store <- read_csv("SourceData/store.csv.zip")

Throws the error:

zip file ... SourceData/store.csv.zip cannot be opened

I prefer to use the temp file, but at this point I'll use either approach if I can make it work.

Community
  • 1
  • 1
boomt
  • 125
  • 1
  • 1
  • 7
  • Sorry, the title should of course be "file" – boomt Nov 04 '15 at 16:16
  • 1
    Might the error be the result of the fact that you have to log in on the website? – horseoftheyear Nov 04 '15 at 16:27
  • The error ''Error in open.connection(file, "rt") : cannot open the connection" may indicate the lack of file where the function expect it. – YCR Nov 04 '15 at 16:38
  • Can you prove that the file is indeed in the folder R reads from? – Roman Luštrik Nov 04 '15 at 17:13
  • 1. The site does require a log-in.2. In the temp file example, there is a file created in the Temp directory that corresponds to the error message "cannot open zip file 'C:\Users\TBOOM_~1\AppData\Local\Temp\RtmpSohNid\file185438603dd7'". In the Download file example, a zip file is created in the directory. Also, if I try to open the downloaded zip file in Windows 10 I "Windows cannot open the compressed folder...the compressed (zip) folder is invalid." – boomt Nov 05 '15 at 17:30

0 Answers0