07. Loading and Saving R Objects and Datasets

This part of the tutorial series applies if you're running R straight from the command line. RStudio should have equivalent GUI options that are not difficult to find. Even if you plan on exclusively using RStudio, this page should still be worth the read.

When you're just about done working for the day, you can close R with the q() command. However, what about all that work you've done and the variables you've created. What if you want to save them for your next session? We may do so with by saving to R's dataset.

Objects in my environment

Before we save our workspace, let's check to see which objects are currently in your environment. Use the ls() command. This will list out all objects created.

> ls()
[1] "sayHello" "x" "y"

Your output will be slightly different, depending on how much tinkering you've done during this tutorial.

Saving and loading by default

By default, when you select the "y" option when you quit (q()), the dataset will be saved to .RData file, located from where you ran the R session from. This file loads automatically when you start R, as long as you start R within the same directory as that file.

Saving and loading manually

We may also save and load to a file manually.

Saving to file

To save an R environment, use the save.image() command with the name of the file as the argument.

> save.image("firstSession.RData")

This will save firstSession.RData onto your current working directory.

Loading an R dataset

To load your current dataset, use the load() command with your filename.

> load("firstSession.RData")

This will load all the variables from your previous session.

Learn to be a Pythonista!

Learn Python in One Day

Learn to be a Pythonista! Try Python

Have you always wanted to learn computer programming but are afraid it'll be too difficult for you? Or you're familiar with some programming but are interested in learning Python fast? Then this book is for you. You no longer have to waste your time and money learning Python from lengthy books, expensive online courses or complicated Python tutorials.

$ Check price
11.9711.97Amazon 4 logo(185+ reviews)

More Python resources

Become a Bioinformatics Whiz!

Introduction to Bioinformatics Vol. 2

Become a Bioinformatics Whiz! Try Bioinformatics

This is Volume 2 of Bioinformatics Algorithms: An Active Learning Approach. This book presents students with a light-hearted and analogy-filled companion to the author's acclaimed course on Coursera. Each chapter begins with an interesting biological question that further evolves into more and more efficiently solutions of solving it.

$ Check price
49.9949.99Amazon 5 logo(5+ reviews)

More Bioinformatics resources

Ad