Lesson 9—Import data via R scripts


First published on April 20, 2019

Learning Objectives

Write code to important data in .R scripts



Instead of using the Import Dataset button in RStudio as we have been doing so far, we will write R codes in our .R script to do the data importing.

The advantage of importing your data via your .R script is that you can re-import your dataset and re-run your script in one smooth operation so as to generate a clean output.


Here are the steps:

Step 1: Bring up the Import Dataset screen

In RStudio,

i) Click the Import Dataset button and choose From Text (readr)….

ii) Then select the dataset that you are going to use BUT DO NOT click the IMPORT button at the bottom of the screen yet.

iii) Instead, copy the lines of R code in the box Code Preview in the lower left-hand corner of the screen and then paste the lines of R code into your .R script.

Here is what your .R script should look like after you paste the lines of R code and save your script:




Content is copyright the author. Layout is copyright Corsbook. See for further notices.