Introduction
These functions help you save and load R objects easily. You can keep your work and use it later without redoing everything.
These functions help you save and load R objects easily. You can keep your work and use it later without redoing everything.
saveRDS(object, file) readRDS(file)
saveRDS() saves a single R object to a file.
readRDS() reads the saved object back into R.
my_data to a file named data.rds.saveRDS(my_data, "data.rds")data.rds into loaded_data.loaded_data <- readRDS("data.rds")new_model.saveRDS(model, "model.rds") new_model <- readRDS("model.rds")
This program creates a list, saves it to a file, then loads it back and prints it.
my_list <- list(name = "Alice", age = 30, scores = c(90, 85, 88)) saveRDS(my_list, "my_list.rds") loaded_list <- readRDS("my_list.rds") print(loaded_list)
The file saved by saveRDS() is binary and only readable by R.
You can save any R object, like vectors, lists, data frames, or models.
Use readRDS() to load the object and assign it to a variable.
saveRDS() saves one R object to a file.
readRDS() loads the saved object back into R.
These functions help keep your work safe and reusable.
saveRDS() function do in R?saveRDS() function is used to save one R object to a file in a binary format.my_data to a file named data.rds?my_list <- list(a = 1, b = 2)
saveRDS(my_list, "mylist.rds")
loaded_list <- readRDS("mylist.rds")
print(loaded_list$b)my_list has elements a=1 and b=2. It is saved to a file and then loaded back into loaded_list.loaded_list$b retrieves the value 2, as saved.saveRDS(data, "datafile.rds") new_data <- readRDS(datafile.rds) print(new_data)
readRDS(datafile.rds), the filename is unquoted, causing an error.df using saveRDS(df, "df.rds"). Later, you want to load it and add a new column status with value "active" for all rows. Which code correctly does this?df <- readRDS("df.rds") to load the saved object into df.df$status <- "active" and save the updated object back with saveRDS(df, "df.rds").Pass the test and this pattern moves unknown