If you're having trouble programming data file imports, try RStudio's code preview

When it comes to importing flat data files stored locally on your computer, such as csv’s or xls’s, you might be uncertain which method to use. It can also be hard to remember how to do it or the options that are available for various file types.

Thankfully, RStudio will suggest code to use to import the files. In the lower right quadrant (the area tabbed with “Files,” “Plots,” “Packages,” “Help,” and “Viewer”) make sure you’re on the “Files” tab and navigate to the folder where your data file is stored. Single click on the file and a little window will pop up. Select “Import Dataset.”

Then a new, larger window will appear that provides a preview of the data itself as well as import options. If the data looks weird or is empty, make sure the import options are correct. For example, make sure the delimiter option is set to comma, tab, semi-colon or whatever the separator is in your file.

Once everything is ready to go, it’s time to finish importing the dataset, including grabbing the code to use in your file or project. You can find the code preview in the lower right corner of this same window. (Note that the code includes the relevant package required to run the code. Make sure that package is installed before you run your code.) Copy that and then click the “Import” button below that preview.

Now just paste your code in your file or project and you’re set to proceed.