Importing Data

Importing data and the here() function

The templates use the function here() in conjunction with import() to locate and import local files.

The import() function

The function import() from the package rio makes it easy to import many types of files.

It uses the file’s extension (e.g. .xlsx, .csv, .dta, etc.) to appropriately read the file. You can read more about import() on this page. If importing an Excel file, the templates use import()’s argument which= to specify the sheet to import.

However, import() needs to be told where to look on your computer to find the file. Therefore, we have wrapped import() around the function here(). Normally, import() accepts a filepath in quotes, but it is easy to make mistakes that way.

The here() function

The function here() from the package Here makes it easy to locate local files.

here() works well within R projects. When the package is first loaded, it automatically considers the top-level folder of your R project as “here” - as a benchmark for all other files in the Project. In your script, if you want to import or reference a file saved in your R project’s folders, you use the function here() to tell R where the file is in relation to that benchmark.

If you are unsure where “here” is set to, run this command:

# This command tells you the folder path that "here" is set to 

Importing using import() and here() together

Below is an example of importing the file “linelist.xlsx”. If the file you want is in the folder designated as “here”, all you have to do is provide the name of the file in quotes (with the appropriate ending).

linelist_raw <- rio::import(here("linelist.xlsx"), which = "Sheet1")

After the closing parenthes of here() the import() function continues. There is a comma and import()’s optional argument which =, specifying which sheet of the Excel file to import.

If however, your file is within subfolders of the folder designated as “here” - let’s say it is in the subfolder “data” and then the sub-subfolder “linelists” - you write these folder names before the file name, in quotes, and separated by commas, as below:

linelist_raw <- rio::import(here("data", "linelists", "linelist.xlsx"), which = "Sheet1")

You can read more about Here and here() in this overview and this documentation

By using the Here package and here() function with R projects, it is not necessary to worry about your “working directory”, setwd(), or getwd() commands.