How To Save & Load An RDS File In R


R is a popular programming language for data analysis, and it’s often used to create and manipulate data objects. RDS (R Data Serialization) files are a common format for saving R objects in RStudio, and they allow you to preserve the state of an object between R sessions. Saving your R object as an RDS file in R can be useful for sharing your work with others, replicating your analysis, or simply storing your work for later use.

In this tutorial, you’ll learn how to save and load objects from an RDS file in R. This blog will also cover how to import objects from R to Power BI.

We will cover the basics of saving and loading RDS files in R. We will explain what RDS files are and how they work, and we will walk you through the process of saving an R object as an RDS file, and then loading that file back into R. We’ll also provide some tips and tricks to help you work with RDS files more efficiently.

Preparing The File

For this demonstration, we’ll be using the Lahman package which contains baseball data. We’ll also be using the tidyverse package for basic analysis.

The Lahman package contains a dataset called Salaries. The goal is to create a table containing information on the year, player name, and their individual and cumulative salaries.

First, let’s assign the Salaries dataset to earnings. Then, filter out unnecessary information from the dataset using the select ( ) function. Next, organize the data by arranging it according to playerID and yearID. The data is further grouped according to playerID.

The pipe operator, denoted by %>%, is used to feed the results of an operation into the next operation below it.

To create a new column containing the cumulative salaries of the players, we need to use the mutate ( ) function along with the cumsum ( ) function. When you run the code, you’ll get a table with information on the year, player, salary, and cumulative salary.

However, it’s difficult to identity players based on ID number alone. We need to add columns that show the player name. Do this by using the select ( ) function. In the code below, the left_join ( ) function is used to join the two tables together.

To view the new table, run the object. The head ( ) function is used to return the first rows in a table.

If you want to check the accuracy of the result, you can use the equality operator which is denoted by two equal signs ( == ).

Saving An RDS File In R

Saving an object as an RDS file in R allows you to work on it later, unlike a file with a CSV extension.

To save a file with an RDS extension, use the write_rds ( ) function. You can see that it belongs to the readr package within Lahman.

rds file in R

In the argument, write the R object’s name and the location where you want to save the file.

rds file in R

If you open a new RStudio session, you can view the object again using the read_rds ( ) function.

rds file in R

Open An Object In Power BI Using An RDS File In R

To open an object with an RDS extension in Power BI, click the Get Data option under the Home tab and select More.

In the Get Data wizard, select R Script and then click Connect.

In the text box, write the library package along with the same line of code used to open the RDS file in RStudio. Then, click OK.

rds file in R

Once the file loads, you can open it in Power Query and make additional changes. But it’s recommended to clean and polish the data and table in RStudio before importing the file to Power BI.

***** Related Links *****
R Scripting For Power BI Using RStudio
Edit Data In R Using The DataEditR Package
R For Power BI | A Beginner’s Guide

Conclusion

An RDS file is a convenient and efficient way to save and share R objects such as data frames, models, and lists. They can be easily read and written, and they are a great alternative to other file formats such as CSV or Excel.

Additionally, RDS files maintain the integrity and structure of R objects, making them ideal for data analysis and modeling projects. They are an essential tool for any R user and can greatly simplify the process of working with data in R.

All the best,

George Mount

 

 



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*