Load or Download a File?


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.

  1. 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.

  2. 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.

    • The function should return the resulting data frame.
    • Add some documentation to the top of the function describing what it does.
    • Call this function using “species.csv” as the file name and https://ndownloader.figshare.com/files/3299483 as the link. Print the first few rows of the resulting data frame using head().