0
0
R Programmingprogramming~10 mins

readRDS and saveRDS in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - readRDS and saveRDS
Create or have an R object
Use saveRDS(object, file)
Object saved as .rds file
Use readRDS(file)
Object loaded back into R
Use or inspect the loaded object
You save an R object to a file with saveRDS, then later read it back with readRDS to restore the object.
Execution Sample
R Programming
x <- c(10, 20, 30)
saveRDS(x, "data.rds")
y <- readRDS("data.rds")
print(y)
This code saves a vector x to a file and then reads it back into y, printing y.
Execution Table
StepActionObject StateFile StateOutput
1Create vector xx = c(10, 20, 30)No file yet
2saveRDS(x, "data.rds")x unchangedFile 'data.rds' created with x data
3readRDS("data.rds") assigned to yx unchanged, y = c(10, 20, 30)File 'data.rds' unchanged
4print(y)x unchanged, y unchangedFile unchanged[1] 10 20 30
💡 Program ends after printing y, which matches original x vector.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
xNULLc(10, 20, 30)c(10, 20, 30)c(10, 20, 30)c(10, 20, 30)
yNULLNULLNULLc(10, 20, 30)c(10, 20, 30)
Key Moments - 3 Insights
Why does saveRDS not create a variable in the environment?
saveRDS only writes the object to a file; it does not assign or change variables in R. See step 2 in execution_table where x stays the same.
Does readRDS automatically overwrite existing variables?
No, readRDS returns the object and you must assign it to a variable. In step 3, y is assigned explicitly.
What happens if the file does not exist when using readRDS?
readRDS will give an error because it cannot find the file to load the object.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of y after step 2?
AFile path string
Bc(10, 20, 30)
CNULL
DError
💡 Hint
Check the 'Object State' column at step 2 where y is not yet assigned.
At which step is the file 'data.rds' created?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'File State' column to see when the file appears.
If you forget to assign readRDS to a variable, what happens?
AThe object is saved to a file
BThe object is printed but not saved in a variable
CAn error occurs
DThe object overwrites x automatically
💡 Hint
Recall that readRDS returns the object but does not assign it unless you do so explicitly.
Concept Snapshot
saveRDS(object, file) saves any R object to a file.
readRDS(file) reads the object back from the file.
You must assign readRDS output to a variable.
saveRDS does not change variables in R.
Files use .rds extension by convention.
Full Transcript
This visual trace shows how saveRDS and readRDS work in R. First, we create a vector x with three numbers. Then, saveRDS writes x to a file named 'data.rds' without changing any variables. Next, readRDS reads the file and returns the saved object, which we assign to y. Finally, printing y shows the original vector. Key points: saveRDS saves to file only, readRDS returns the object and must be assigned. If the file is missing, readRDS errors. This helps save and load R objects easily.