While constructing expressions to put in the j
-slot of a [.data.table
call, it would often be helpful to be able to examine and play around with the contents of .SD
.
This naive attempt doesn't work...
library(data.table)
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
DT[, browser(), by=x]
# Called from: `[.data.table`(DT, , browser(), by = x)
Browse[1]>
Browse[1]> .SD
# NULL data.table
... even though a variable named .SD
and several others related to the current data.table subset are all present in the local environment
Browse[1]> ls(all.names = TRUE)
# [1] ".BY" ".GRP" ".I" ".iSD" ".N" ".SD"
# [7] "Cfastmean" "mean" "print" "x"
Browse[1]> .N
# [1] 3
Browse[1]> .I
# [1] 4 5 6
Using .I
, I can view something +/- like .SD
, but it would be nice to be able to directly access its value:
Browse[1]> DT[.I]
# x y v
# 1: b 1 4
# 2: b 3 5
# 3: b 6 6
My questions: Why is the expected value of .SD
not directly available from within a browser()
call (while .I
, .N
, .GRP
and .BY
are)? Is there some alternative way to access the value of .SD
?