unzip in an R package

I currently have some code in an R package that unzips and reads in a CSV file. Reproducible example here:

url <- paste0("http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Momentum_Factor_CSV.zip")

temp <- tempfile()

data <- data.table::fread(unzip(file.path(temp)),fill=TRUE,skip=13)

This leaves a CSV file in my working directory. What is the best practice way to do this without leaving a CSV in the users directory? I've tried unzipping to a temp file but fread doesn't seem to like reading that in.


In my case the code seems to work, but it might be related to file permissions set in the temp folder you have..

I also can think of another way of doing all of it in the working directory

url <- paste0("http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Momentum_Factor_CSV.zip")
download.file(url, "myFile.zip", quiet=TRUE,mode="wb")
data <- data.table::fread(unzip(file.path("myFile.zip")),fill=TRUE,skip=13)

Of course you can make it safer my checking if the file myFile.zip does not exist yet using the file.exists function

Hope this helps,

Thanks for the reply. My code does get rid of the zip file but I am still left with the unzipped CSV file (F-F_Momentum_Factor.CSV) in my working directory. I am trying to find a solution to stop the unzipped file from going to the working directory and ideally without having to know the name of the file in advance as I could obviously just use file.remove on the unzipped file if I know the name in advance.


My bad, I didn't get the part where you wanted to get rid of the csv file :slight_smile:

Would this work?

url <- paste0("http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Momentum_Factor_CSV.zip")

tempDir <- tempdir()
tempFile <- tempfile()


data <- data.table::fread(unzip(file.path(tempFile), exdir = tempDir),fill=TRUE,skip=13)

Here, I unzip to a temp folder and a temp file without ever having to know the name of the file itself. You can see in the temp folder/file assigned to the variables in case you would like to double check.

Hope this helps,

1 Like

That is it. Nice and simple. Thanks.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.