Book Image

R High Performance Programming

By : Aloysius Shao Qin Lim
Book Image

R High Performance Programming

By: Aloysius Shao Qin Lim

Overview of this book

This book is for programmers and developers who want to improve the performance of their R programs by making them run faster with large data sets or who are trying to solve a pesky performance problem.
Table of Contents (12 chapters)
11
Index

Swapping active and nonactive data


In some situations, large objects that are removed to free up memory are needed later in the program. R provides tools to save data to the disk and reload them later when enough memory is available. Returning to the retail sales data example, suppose that we need the sales.data data frame for further processing after mining for frequent itemsets. We can save it to the disk using saveRDS() and reload it later using readRDS():

trans.list <- split(sales.data$item, sales.data$trans)
saveRDS(sales.data, "sales.data.rds")
rm(sales.data)
trans.arules <- as(trans.list, "transactions")
rm(trans.list)
freq.itemsets <- apriori(trans.arules, list(support = 0.3))
sales.data <- readRDS("sales.data.rds")
# Perform further processing with sales.data

The saveRDS() and readRDS() functions save one object at a time without the name of the object. For example, the name sales.data is not saved. However, the column names trans and items are saved. As an alternative...