R Create DataFrame from Files pattern inside a Zip File

Viewed 47

I have been able to download the following .zip file using this code

library(curl)
file_destination <- "C:/Documents/c3.zip"
curl_fetch_disk(url = "https://api.esios.ree.es/archives/9/download?date_type=datos&start_date=01-01-2019&end_date=31-01-2019",
                    path = file_destination )

What I a missing?

1) I just would like to download to a Temp.file instead a local directory, I guess replacing file_destination with

   file_destination <- tempfile()

  

2) I need to create a dataframe from all CSV files that match this name pattern "C3_cuotaven*.csv" :

   2.1 name starts with "C3_cuotaven" 
 
   2.2 and ends with ".csv"

Please help

1 Answers

I have been able to produce this solution

library(curl)
# Create a temporarly file and Temporaly directory
temp_direct <- tempdir()
temp <- tempfile()

#Download ZIP file from web
curl_fetch_disk(url = "https://api.esios.ree.es/archives/9/download?date_type=datos&start_date=01-01-2020&end_date=31-08-2020",
                path = temp)
# Unzip downloaded file in temporaly directory, this creates several ZIP files in it.
unzip(temp, exdir =temp_direct )

# Create a list of files that contain C3 in filename.
zip_c3_pattern <- list.files(path = temp_direct, pattern = '*C3*', full.names = TRUE)
library(plyr)

# unzip all your files with previous pattern
llply(.data = zip_c3_pattern, .fun = unzip, exdir = temp_direct)

# Create a vector list with files that contain this key in their name "C3_cuotaven"
csv_cuotaven <- list.files(path = temp_direct, pattern = '*C3_cuotaven*', full.names = TRUE)

library(plyr)

# Create a dataframe with files that are contain in csv_cuotaven following name pattern = "c3_cuotaven"
library(data.table)
c3_merge <- rbindlist(lapply(csv_cuotaven,  
                             function(x) cbind(fread(x, sep=';', header=FALSE,
                                                     stringsAsFactors=FALSE),
                                               file_nm=x)), fill=TRUE )
#Remove temporaly file from memory
unlink(temp)

# Remove files from directory that start with C3 or file
files <- list.files(temp_direct, full.names = T, pattern ="^C3|^file")
file.remove(files)
Related