File tree Expand file tree Collapse file tree 2 files changed +26
-10
lines changed
Expand file tree Collapse file tree 2 files changed +26
-10
lines changed Original file line number Diff line number Diff line change @@ -61,12 +61,20 @@ setDT(X, check.names=TRUE)[]
6161
6262# FAQ: Dealing with data.table loaded from RDS or RData files
6363# Example to demonstrate the usage of setDT after loading from RDS
64- if (file.exists(" my_data.rds" )) {
65- X = readRDS(" my_data.rds" ) # Load data.table from RDS if file exists
66- setDT(X ) # Make sure to use setDT() after loading
67- } else {
68- message(" my_data.rds file does not exist. Skipping RDS loading example." )
64+ # Step 1: Define file path
65+ rds_file <- " my_data.rds"
66+ # Step 2: Create and save data.table if the file does not exist
67+ if (! file.exists(rds_file )) {
68+ X <- data.table(a = 1 : 5 , b = letters [1 : 5 ]) # Create a data.table
69+ saveRDS(X , rds_file ) # Save it to an RDS file
70+ message(" Saved data.table to " , rds_file )
6971}
72+ # Step 3: Load the data.table from the RDS file
73+ X_loaded <- readRDS(rds_file )
74+ # Step 4: Restore data.table attributes
75+ setDT(X_loaded )
76+ # Verify structure
77+ print(X_loaded )
7078}
7179\keyword { data }
7280
Original file line number Diff line number Diff line change @@ -57,11 +57,19 @@ truelength(DT)-length(DT) # 2047 slots spare
5757
5858# FAQ: Dealing with data.table loaded from RDS or RData files
5959# Example to demonstrate the usage of setDT after loading from RDS
60- if (file.exists("my_data.rds")) {
61- X = readRDS("my_data.rds") # Load data.table from RDS if file exists
62- setDT(X) # Make sure to use setDT() after loading
63- } else {
64- message("my_data.rds file does not exist. Skipping RDS loading example.")
60+ # Step 1: Define file path
61+ rds_file <- "my_data.rds"
62+ # Step 2: Create and save data.table if the file does not exist
63+ if (!file.exists(rds_file)) {
64+ X <- data.table(a = 1:5, b = letters[1:5]) # Create a data.table
65+ saveRDS(X, rds_file) # Save it to an RDS file
66+ message("Saved data.table to ", rds_file)
6567}
68+ # Step 3: Load the data.table from the RDS file
69+ X_loaded <- readRDS(rds_file)
70+ # Step 4: Restore data.table attributes
71+ setDT(X_loaded)
72+ # Verify structure
73+ print(X_loaded)
6674}
6775\k eyword{ data }
You can’t perform that action at this time.
0 commit comments