15 RStudio’s data import tool

In previous chapters, we learned how to programmatically import data into R. In this chapter, I will briefly introduce you to RStudio’s data import tool. Conceptually, I won’t be introducing anything you haven’t already seen before. I just want to make you aware of this tool, which can be a welcomed convenience at times.

For this example, we will use the import tool to help us import the same height and weight csv file we imported in the chapter on importing plain text files – comma.csv.

You may click here to download this file to your compter.

To open RStudio’s data import tool, click the Import Dataset dropdown menu near the top of the environment pane.

Next, because this is a csv file, we will choose the From Text (readr) option from the dropdown menu. The difference between From Text (base) and From Text (readr) is that From Text (readr) will use functions from the readr package to import the data and From Text (base) will use base R functions to import the data.

After you select a file type from the import tool dropdown menu, a separate data import window will open.

At this point, you should click the browse button to locate the file you want to import.

Doing so will open your operating system’s file explorer window. Use that window to find and select the file you want to import. Again, I am using comma.csv for this demonstration.

After selecting you file, there will be some changes in the data import window. Specifically,

  • The file path to the raw data you are importing will appear in the File/URL field.

  • A preview of how R is currently parsing that data will appear in the Data Preview field.

  • Some or all of the import options will become available for you to select or deselect.

  • The underlying code that R is currently using to import this data is displayed in the Code Preview window.

  • The copy to clipboard icon becomes clickable.

Importing this simple data set doesn’t require us to alter many of the import options. However, I do want to point out that you can change the variable type by clicking in the column headers in the Data Preview field. After clicking, a dropdown menu will display that allows you to change variable types. This is equivalent to adjusting the default values passed to the col_types argument of the read_csv() function.

I will go ahead and change the ht_in and wgt_lbs variables from type double to type integer using the dropdown menu.

At this point, our data is ready for import. You can simply press the Import button in the bottom-right corner of the data import window. However, I am going to suggest that you don’t do that. Instead, I’m going to suggest that you click the clipboard icon to copy the code displayed in the Code Preview window and then click the Cancel button.

Next, return to your R script or R Markdown file and paste the code that was copied to your clipboard. At this point, you can run the code as though you wrote it. More importantly, this code is now a part of the record of how you conducted your data analysis. Further, if someone sends you an updated raw data set, you may only need to update the file path in your code instead of clicking around the data import tool again.

That concludes the portion of the book devoted to importing data. In the next chapter, we will discuss strategies for exporting data so that you can store it in a more long-term way and/or share it with others.