1. Home
  2. Training Library
  3. 9. Implicit Control Flow in R

Saving and loading all objects in R

Developed with
QA

The course is part of this learning path

Fundamentals of R
course-steps
11
certification
3
lab-steps
1
description
1
play-arrow
Saving and loading all objects in R
Overview
DifficultyBeginner
Duration31m
Students8
Ratings
5/5
starstarstarstarstar

Description

Course Description 

This module looks at how to control data in R, through reading, writing and loading objects in R.  

Learning Objectives 

The objectives of this module are to provide you with an understanding of: 

  • How to bring in data from a file in R  
  • Saving and loading objects in R  
  • Interacting with the clipboard  
  • How to connect to files in R  
  • How to read from a file in R  
  • How to write to a file in R  

Intended Audience 

Aimed at all who wish to learn the R programming language. 

Pre-requisites 

No prior knowledge of R is assumed 

Delegates should already be familiar with basic programming concepts such as variables, scope and functions 

Experience of another scripting language such as Python or Perl would be an advantage 

Understanding mathematical concepts will be beneficial 

Feedback 

We welcome all feedback and suggestions - please contact us at qa.elearningadmin@qa.com to let us know what you think. 

Transcript

- . We can load and save objects in R. For example an object would be maybe a TEST vector. I can run the save function on this and save this down in a specified file. Here we write an external representation of this R object. And I have now created temp.text which will contain TEST. I can open up this temp.text file in a notepad window and show you that this is an external representation not recognized by a Windows notepad. I can update my test variable into say for example waiting. And I can now reload in the data set that has been saved down by the function save using the function load. By doing so, I update the test variable in the global environment with the contents of the temp.text which has been created as the original test vector ranging from one to 10. I can save down multiple objects. So let me try and create another variable. And here now I'll use the preferred file extension rather than the text file extension which was perhaps incorrect. Given that the external representation of this file in the preferences to use RData. So now my file temp.RData contains two variables that I have created. And you could list as many as you wanted here. Let us overwrite TEST with the keyword waiting just so we can see the global environment being updated after having run the load temp.RData file. We can see that the update has occurred. I could have saved this down with a shortened version of the RData file. You may come across RDA files. They are merely short names for RData files. Again once again I can show you... I can update TEST and I can run the load on this to show you once more that the load updates the TEST variable. By clicking on the file in the file explorer in the right, bottom right pane, I can open up a... So if I updated my test to waiting, now if I was to click on any one of these three files here, if I click on RData we get the option of, "Do you want to load the R Data file into the environment?" And the answer is yes. So the benefit of using an RData file is that you have the ability to then load in this recognized external representation of an R object back into your global environment using your mouse. Here let us note that the load function uses the relative path. And by using the mouse clicker we've ended up seeing where our relative path lives. What if I wanted to save down not just a couple of variables, canal and test, but many variables? Now, let's say I had... Let me clear out my messy concepts first that I have. So let's list them to the screen. And then I can remove all of them using the remove command rm. Where it takes an input of a list of all of the variables that I have defined in my space. I could have done this manually had I wanted to click on the paintbrush icon in the environments tab to allow me to manually delete all objects in my R working environment. And I can choose to include hidden objects or not. For now I'll click yes. So that's cleared out my environment. Let us create some data that we would like to save down. Now, imagine we were busy working away and we had created a couple of very important matrices and some strings and saved them down. And now our global environment is populated with important results that I'd like to save down. Let us try and save down this workspace. So I'd like to save down everything. And the reason to stress that is because in the save command, I'm going to use the list of every single name that I have. All names equals true in this function here LS includes hidden variables. Not just those that we can see in the global environment Window Explorer, but also those that we can't see. And I'd like to save this down as the file name workspace Rdata. In order to prove that I have created a copy of all of the variables in my global environment, I will click on the paintbrush. Then I can click remove including hidden objects yes. Now, I would like to show you that we can load this back in using the load function. And here we just have to type in the name of the workspace 2020.Rdata file. And ensuring that we have typed this in correctly and it mirrors what we had saved Rdata down as. We can see that we have the same global environment created. Now, if I look at the variables that I had created during my important working period, if I copy the name of result, I can see that we have the same result again. And if I copy the character data, we can see that we have the same character data again. We can even ask for the list of all the names that have been loaded just to understand that no extra variables got picked up along the way. It is noteworthy to say there is a shortcut to the save function for our workspace which is save.image. And if you see in the file explorer when I hit enter here we can see that we have created a .Rdata file which has no name. So every time you're working you might see this file up here. In our studio it seems to have an ability to create this regularly just in case things go wrong. It stores a workspace image. Here we have... using the file parameter of the save.image shortcut function of the save list all function. This allows me to save it as exactly the same filename as before. And hence I've overwritten the previous workspace. I could have done this manually as a side note by clicking on session. Clicking on save workspace as. This would allow me a choice of workspace here that I can save it as. I could have also if I'd wanted to click on inside the environments tab on the same icon here. It would bring up the same pop-up. .

About the Author

Students219
Labs1
Courses11
Learning paths1

Kunal has worked with data for most of his career, ranging from diffusion markov chain processes to migrating reporting platforms.  

Kunal has helped clients with early stage engagement and formed multi week training programme curriculum. 

Kunal has a passion for statistics and data; he has delivered training relating to Hypothesis Testing, Exploring Data, Machine Learning Algorithms, and the Theory of Visualisation. 

Data Scientist at a credit management company; applied statistical analysis to distressed portfolios. 

Business Data Analyst at an investment bank; project to overhaul the legacy reporting and analytics platform. 

Statistician within the Government Statistical Service; quantitative analysis and publishing statistical findings of emerging levels of council tax data. 

Structured Credit Product Control at an investment bank; developing, maintaining, and deploying a PnL platform for the CVA Hedging trading desk.