0
0
R Programmingprogramming~20 mins

readRDS and saveRDS in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
R readRDS/saveRDS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this R code using saveRDS and readRDS?

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?

R Programming
my_list <- list(a = 1, b = 2)
saveRDS(my_list, file = "temp.rds")
new_list <- readRDS("temp.rds")
print(new_list$b)
A2
BNULL
CError: object 'new_list' not found
D1
Attempts:
2 left
💡 Hint

Remember that saveRDS saves the object exactly and readRDS reads it back as is.

🧠 Conceptual
intermediate
1:30remaining
What does saveRDS() do in R?

Which of the following best describes the purpose of saveRDS() in R?

AConverts an R object to a CSV file
BSaves an R object to a file in a binary format for later use
CReads an R object from a file and loads it into the global environment
DSaves all variables in the current environment to a text file
Attempts:
2 left
💡 Hint

Think about what saveRDS() stores and how it differs from save().

🔧 Debug
advanced
2:00remaining
Why does this code cause an error when reading an RDS file?

Look at this R code:

saveRDS(42, file = "number.rds")
num <- readRDS("number.txt")
print(num)

What error will occur and why?

AError: object 'num' not found
BNo error, prints 42
CError: cannot open the connection to 'number.txt' because the file does not exist
DError: invalid R object in 'number.txt'
Attempts:
2 left
💡 Hint

Check the file names used in saving and reading.

📝 Syntax
advanced
2:00remaining
Which option correctly saves and reads an R object?

Which of the following code snippets correctly saves an object df and reads it back into df2?

A
saveRDS(df, "data.rds")
df2 &lt;- readRDS("data.rds")
B
saveRDS(df)
df2 &lt;- readRDS()
C
saveRDS(df, "data.txt")
df2 &lt;- readRDS("data.txt")
D
saveRDS(df, file = "data.rds")
df2 &lt;- readRDS(file = "data.rds")
Attempts:
2 left
💡 Hint

Check the correct argument names and file extensions.

🚀 Application
expert
2:30remaining
How many objects are saved and restored using saveRDS and readRDS?

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?

AOne object: only 'a' is saved and restored
BTwo objects: both 'a' and 'b' are saved and restored
CNo objects are saved because 'b' is not saved explicitly
DAll objects in the environment are saved and restored
Attempts:
2 left
💡 Hint

Remember what saveRDS saves compared to save.