Skip to content

Commit bbdf158

Browse files
venom1204ben-schwenaitapMichaelChirico
authored
"Enhanced setDT Documentation: Added Example for RDS/RData Usage and FAQ Link" (#6894)
* faq updated * updated example * Update vignettes/datatable-faq.Rmd Co-authored-by: Benjamin Schwendinger <[email protected]> * final * Update man/setDT.Rd Co-authored-by: aitap <[email protected]> * updated example * style * updated example * example updated * Update man/truelength.Rd Co-authored-by: Benjamin Schwendinger <[email protected]> * Update man/setDT.Rd --------- Co-authored-by: Benjamin Schwendinger <[email protected]> Co-authored-by: aitap <[email protected]> Co-authored-by: Michael Chirico <[email protected]>
1 parent 8785804 commit bbdf158

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

man/setDT.Rd

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ setDT(x, keep.rownames=FALSE, key=NULL, check.names=FALSE)
2828
}
2929
3030
\seealso{
31-
\code{\link{data.table}}, \code{\link{as.data.table}}, \code{\link{setDF}}, \code{\link{copy}}, \code{\link{setkey}}, \code{\link{setcolorder}}, \code{\link{setattr}}, \code{\link{setnames}}, \code{\link{set}}, \code{\link{:=}}, \code{\link{setorder}}
31+
\code{\link{data.table}}, \code{\link{as.data.table}}, \code{\link{setDF}}, \code{\link{copy}}, \code{\link{setkey}}, \code{\link{setcolorder}}, \code{\link{setattr}}, \code{\link{setnames}}, \code{\link{set}}, \code{\link{:=}}, \code{\link{setorder}},
32+
See the FAQ vignette: \code{vignette("datatable-faq", package = "data.table")}.
3233
}
3334
\examples{
3435
@@ -58,6 +59,14 @@ setDT(X, key="a")[]
5859
X = list(a=1:5, a=6:10)
5960
setDT(X, check.names=TRUE)[]
6061

62+
# Example demonstrating setDT after loading from RDS
63+
rds_file = tempfile(fileext = ".rds")
64+
X = data.table(a = 1:5, b = letters[1:5])
65+
saveRDS(X, rds_file)
66+
X_loaded = readRDS(rds_file)
67+
setDT(X_loaded) # restore internal data.table attributes
68+
print(X_loaded)
69+
unlink(rds_file)
6170
}
6271
\keyword{ data }
6372

vignettes/datatable-faq.Rmd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,10 @@ DT[ , b := rnorm(5)] # 'replace' integer column with a numeric column
620620

621621
## Reading data.table from RDS or RData file
622622

623-
`*.RDS` and `*.RData` are file types which can store in-memory R objects on disk efficiently. However, storing data.table into the binary file loses its column over-allocation. This isn't a big deal -- your data.table will be copied in memory on the next _by reference_ operation and throw a warning. Therefore it is recommended to call `setalloccol()` on each data.table loaded with `readRDS()` or `load()` calls.
623+
`*.RDS` and `*.RData` are file types which can store in-memory R objects on disk efficiently. However, storing `data.table` into a binary file loses its column over-allocation (see also `?truelength`). This isn't a big deal -- your `data.table` will be copied in memory on the next _by reference_ operation and throw a warning.
624+
Therefore, it is recommended to call `setDT()` on each `data.table` loaded with `readRDS()` or `load()` calls to restore its internal attributes. If you only need to pre-allocate space for new columns, `setalloccol()` can also be used.
625+
626+
For more details, see `?setDT` and `?truelength`.
624627

625628
# General questions about the package
626629

0 commit comments

Comments
 (0)