In a previous blog, I wrote about how you could use R to unzip files. Well, that raises the question, can I download a zip file and then unzip it using R?
You can view the previous blog post on how to unzip files here.
Update: YouTube video can be found at the bottom of this post!
Download files using R
In general, downloading and processing a file can be done in Power BI very easily using “web” as the data source and you never have to worry about using R. However, if you need to do something more complex then R can be very helpful. For example, you may want to download a zip file, unzip it, and then process it.
In this example, I am going to download a zip file that contains names by state, this zip file is stored in the following location:
https://www.ssa.gov/oact/babynames/state/namesbystate.zip
This zip file has data for each of the different states, for this example I only want to look at the data for Florida. So we are going to unzip the file and only extract the data from the single file that we need data from.
Let’s take a look at the code in R Studio and break it down:
- First, I want to store the destination location and filename in a variable, this is done in lines 1-2.
- Next, I want to download the zip file and store it locally, this is done in lines 4-6.
- Thirdly, now the file will be unzipped and the file FL.TXT will be stored in the variable FLnames, lines 8-9.
- Lastly, we will read the text file and store it in the variable babynames.
The only thing we added in this example that was not in the previous blog post was the ability of downloading the code. If you, missed the previous blog and want to know how to import the data into Power BI, you can check that our here.
Thanks, for checking out my blog!