7) Importing a CSV file with R Studio.

This Blog entry is from the Loading and Shaping section in Learn R.

RStudio offers a simple GUI user interface to load files into Data Frames.  The functionality is of course distinct to RStudio but in practice it is a code creator that uses the read.table() function to load a variety of common file formats to a Data Frame.

The procedure here in will use the datasets contained in the bundle.  In this procedure, the csv datasets contained in \Bundle\Data\Equity\Equity will be targeted:


Specifically, the AAPL.csv file which contains a series of prices relating to the Apple share price:


In RStudio, navigate to the Import Dataset button in the top right-hand corner of the screen, above the environment pane:


Click the button Import Dataset:


Click the From CSV sub menu:


The Import Text file window will expand.  Click the browse button in the top right-hand corner of the window to open the file system navigator:


Navigate to Bundle\Data\Equity\Equity\AAPL.csv and click the Open button:


A preview of the file is show in the window for the purposes of validation:


As is the case with many RStudio functions it is in essence a macro or code creation widget.  It can be seen in the bottom right hand corner that RStudio has created the corresponding R script block that will be responsible for importing the file in the console:


In this example, it can be observed that the readr package is being loaded, the csv file is being loaded to a data frame called AAPL using the read_csv function.  The readr is a more efficient package for the importing and exporting of data created by the RStudio team and while there are several functions for the import and export of data native to R, these are not especially performant.  It is worth noting that this package WILL NOT convert strings to factors, making it a more labour-intensive choice for text rich datasets that are intended to be the source of predictive analytics methods.

Towards the bottom left hand corner of window is additional parameters available in the creation of the csv file.


Simply click import to load the data into the R session:


It can be seen that the block of script has been run to console, that the AAPL data frame is now available in the environment pane and care of the View() function, that the data frame has been displayed in a tab of the script pane:


It is important to note that all RStudio had done is create a block of R script and executed this to console.  In the interests of reproducibility and in a script active console passive methodology, this block of script should be reproduced directly in a script.  By way of standard, the readr package will be used in most, but not all, importing methods.

Expanding on the data frame it can be observed that the readr package has facilitated the creation of the correct object types:


In this case, it can be seen that the handling of dates has taken place via POSIXCT, which is an alternative date handling object as detailed in procedure 43.

21) Loading .Rdata from file.

This Blog entry is from the Data Structures section in Learn R.

To fully demonstrate the process of loading objects from an RData file fully close down RStudio by clicking File,  then upon the menu expanding,  clicking Quit Session or by clicking on the close button in the top right hand corner:


As expected from other procedures confirmation will be sought about the treatment of the current session.  Elect not to save the session by clicking "Don’t Save":


Upon termination of RStudio,  simply reload:


It can be seen that there are no objects loaded.  Assuming the working directory is unchanged, to load the objects saved in procedure 38, simply type:


Run the line of script to console:


The objects saved previously are promptly loaded and available in the environment pane of RStudio and by implication available for recall in scripts and \ or the console.


As R has several programmatic implementations,  such as R.net which is used for real-time invocation,  the saving and loading of R sessions provides a useful means to be able to deploy objects.