How to save multiple R objects to a file with other data?

Viewed 4701

This is for a tool I am building, so my solution can't be adhoc. I would like to save a ggplot object to a log file, which contains a lot of other information. Then I would use many of these log files to make a combined plot of all the ggplots. I've tried just saving the images as .pngs and then combining them in R but the quality decreases significantly when combining them from image files. Any ideas?

I don't want to save them to indiviual .Rdata files because I'd like all the information to be contained within the log file. Is my only option to save the dataframe used to construct the ggplot and then reconstruct it later?

1 Answers

I am not sure the reason for embedding in a log file, but you have two options here with saving r objects.

Option1. Save several memory objects at once.

save(ggplot1object, dataframe2, dataframe3, file = "location.filename.Rdata")

Then you can

load("location.filename.RData") 

and all 3 objects will be loaded into memory.

Option 2. Create a List and save the list.

save(list(ggplot1object, dataframe2, dataframe3), "location.filename.Rdata")

Then you can

load("location.filename.Rdata")

and the single list item with the 3 different pieces will be loaded into the environment. These can be ggplot output items like p1, p2, etc... that represent different plot objects.

Related