With large data files, it can be useful to only download the file if it hasn’t already been downloaded. One way to do this is to check if the file name exists in your working directory. If it does then load it, if not then download it.
You can use the list.files()
function to get a list of files and directories in the
working directory and the download.file(url, filename)
function to download
the file from a URL give in url
with a specific file name given in filename
.
Write a conditional statement that checks if surveys.csv
exists in the
working directory, if it doesn’t then downloads it from
https://ndownloader.figshare.com/files/2292172 using download.file()
, and finally
loads the file into a data frame and displays the first few rows using the head()
function. The url needs to be in quotes since it is character data.
Make a version of this conditional statement that is a function, where the name of the file is the first argument and the link for downloading the file is the second argument.
head()
.