1

I am trying to read a csv file that is contained in a file I extracted from the web. The problem is the zipped file has multiple cascading folders. I have to do that for several different units, so I am performing a loop. There is no problem with the loop, the file name is correct and I get to download the file. However I get an error message (and I think is because R cannot find the exact file I am asking it to find). The error is:

Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
  cannot locate file 'XXXX.csv' in zip file 'c:\yyy\temp\bla\'


download.file(paste("http://web.com_",units[i],"_",places[j],".zip",
                     sep=""),
                     temp,
                     cacheOK = F )
data <- read.csv2(unz(temp,
                   paste("name_",units[i],"_",places[j],".csv",
                   sep="")),
                   header=F,
                   skip=1)
unlink(temp)
fili<-rbind(X,
            data)

}

How do I make R find the file I want?

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
Lucarno
  • 393
  • 4
  • 14
  • I have never worked with zipped data in R but can you do some combination of `list.files()` with `full.names=TRUE` and `recursive=TRUE` on a zip to find the right file? Once you have a directory listing you could `grep` to find the full path to the right csv file. See this question too: http://stackoverflow.com/questions/8986818/automate-zip-file-reading-in-r – thelatemail May 24 '12 at 01:33

2 Answers2

1

You have the right approach but (as the warning tells you) the wrong filename.

It's worth double checking that the zip file does exist before you start trying to read its contents.

if(file.exists(temp))
{
  read.csv2(unz(...))
} else
{
  stop("ZIP file has not been downloaded to the place you expected.")
}

It's also a good idea to a browse around inside the downloaded file (you may wish to unzip it first) to make sure that you are looking in the right place for the CSV contents.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
0

It looks like the file, you're going to read, is located in directory. In this case your reading should be changed as follows:

data <- read.csv2(unz(temp,
                   paste("**dirname**/name_",units[i],"_",places[j],".csv",
                   sep="")),
                   header=F,
                   skip=1)
Anton K
  • 4,658
  • 2
  • 47
  • 60