Consider the following R code that saves and reads an object:
my_list <- list(a = 1, b = 2)
saveRDS(my_list, file = "temp.rds")
new_list <- readRDS("temp.rds")
print(new_list$b)What will be printed?
my_list <- list(a = 1, b = 2) saveRDS(my_list, file = "temp.rds") new_list <- readRDS("temp.rds") print(new_list$b)
Remember that saveRDS saves the object exactly and readRDS reads it back as is.
The object my_list is saved to a file and then read back into new_list. Accessing new_list$b returns 2, the value stored under key 'b'.
Which of the following best describes the purpose of saveRDS() in R?
Think about what saveRDS() stores and how it differs from save().
saveRDS() saves a single R object to a file in a binary format. It does not save the entire environment or convert to text formats.
Look at this R code:
saveRDS(42, file = "number.rds")
num <- readRDS("number.txt")
print(num)What error will occur and why?
Check the file names used in saving and reading.
The file was saved as 'number.rds' but the code tries to read 'number.txt', which does not exist, causing a file connection error.
Which of the following code snippets correctly saves an object df and reads it back into df2?
Check the correct argument names and file extensions.
Option D uses the correct argument file= and proper file extension .rds. Option D misses the argument name, which is allowed but less clear. Option D uses wrong extension, and B misses file arguments.
Given the following R code:
a <- 10
b <- 20
saveRDS(a, file = "a.rds")
saved_obj <- readRDS("a.rds")How many objects are saved and restored by saveRDS and readRDS?
Remember what saveRDS saves compared to save.
saveRDS saves only the single object passed to it, here 'a'. The object 'b' is not saved or restored.