From 09da96104a571cdffdbebef6208aef3eb699352d Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Fri, 1 Jan 2021 14:40:32 -0500 Subject: [PATCH 1/3] remove an obsolete reference from the FAQ vignette --- vignettes/datatable-faq.Rmd | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/vignettes/datatable-faq.Rmd b/vignettes/datatable-faq.Rmd index e0cd81b343..f78d5a2aac 100644 --- a/vignettes/datatable-faq.Rmd +++ b/vignettes/datatable-faq.Rmd @@ -68,9 +68,20 @@ The `j` expression is the 2nd argument. Try `DT[ , c("x","y","z")]` or `DT[ , .( ## I assigned a variable `mycol = "x"` but then `DT[ , mycol]` returns `"x"`. How do I get it to look up the column name contained in the `mycol` variable? -In v1.9.8 released Nov 2016 there is an ability to turn on new behaviour: `options(datatable.WhenJisSymbolThenCallingScope=TRUE)`. It will then work as you expected, just like data.frame. If you are a new user of data.table, you should probably do this. You can place this command in your .Rprofile file so you don't have to remember again. See the long item in release notes about this. The release notes are linked at the top of the data.table homepage: [NEWS](https://github.com/Rdatatable/data.table/blob/master/NEWS.md). +What's happening is that the `j` expression sees objects in the calling scope. The variable `mycol` does not exist as a column name of `DT` so `data.table` then looked in the calling scope and found `mycol` there and returned its value `"x"`. This is correct behaviour currently. Had `mycol` been a column name, then that column's data would have been returned. -Without turning on that new behaviour, what's happening is that the `j` expression sees objects in the calling scope. The variable `mycol` does not exist as a column name of `DT` so data.table then looked in the calling scope and found `mycol` there and returned its value `"x"`. This is correct behaviour currently. Had `mycol` been a column name, then that column's data would have been returned. What has been done to date has been `DT[ , mycol, with = FALSE]` which will return the `x` column's data as required. That will still work in the future, too. Alternatively, since a data.table _is_ a `list`, too, you have been and still will be able to write and rely on `DT[[mycol]]`. +To get the column `x` from `DT`, there are a few options: + +```{r metaprogram_column} +# using .. to tell data.table the variable should be evaluated +DT[ , ..mycol] +# using with=FALSE to do the same +DT[ , mycol, with=FALSE] +# treating DT as a list and using [[ +DT[[mycol]] +``` + +The `with` argument refers to the `base` function `with` -- when `with=TRUE`, `data.table` operates similar to `with`, i.e. `DT[ , mycol]` behaves like `with(DT, mycol)`. When `with=FALSE`, the standard `data.frame` evaluation rules apply. ## What are the benefits of being able to use column names as if they are variables inside `DT[...]`? From 6c66843a7c331fd95cad65eb0a46f98805a8a446 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Fri, 1 Jan 2021 14:46:44 -0500 Subject: [PATCH 2/3] need eval=FALSE --- vignettes/datatable-faq.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/datatable-faq.Rmd b/vignettes/datatable-faq.Rmd index f78d5a2aac..8f73c895d1 100644 --- a/vignettes/datatable-faq.Rmd +++ b/vignettes/datatable-faq.Rmd @@ -72,7 +72,7 @@ What's happening is that the `j` expression sees objects in the calling scope. T To get the column `x` from `DT`, there are a few options: -```{r metaprogram_column} +```{r metaprogram_column, eval=FALSE} # using .. to tell data.table the variable should be evaluated DT[ , ..mycol] # using with=FALSE to do the same From ba3495335fa78e18f37c64215f2044090c76f8c3 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sat, 2 Jan 2021 17:02:22 -0500 Subject: [PATCH 3/3] move to code block --- vignettes/datatable-faq.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/datatable-faq.Rmd b/vignettes/datatable-faq.Rmd index 8f73c895d1..816cb99882 100644 --- a/vignettes/datatable-faq.Rmd +++ b/vignettes/datatable-faq.Rmd @@ -72,7 +72,7 @@ What's happening is that the `j` expression sees objects in the calling scope. T To get the column `x` from `DT`, there are a few options: -```{r metaprogram_column, eval=FALSE} +```r # using .. to tell data.table the variable should be evaluated DT[ , ..mycol] # using with=FALSE to do the same