How to add a new column in Data.table without generating warning message

When I want to create a new table in the new versions of data.table it generated an error which is annoying because I am logging errors and warnings in my shiny app

iris %>% setDT()
iris[,asdf:= '']
iris[,gsd:=character()]
iris[,':='(vik = character())]

All of these ways give me a warning message like this

> iris[,asdf:= '']
Error in assign(as.character(name), x, parent.frame(), inherits = TRUE) : 
  cannot change value of locked binding for 'iris'
In addition: Warning message:
In `[.data.table`(iris, , `:=`(asdf, "")) :
  Invalid .internal.selfref detected and fixed by taking a (shallow) copy of the data.table so that := can add this new column by reference. At an earlier point, this data.table has been copied by R (or was created manually using structure() or similar). Avoid key<-, names<- and attr<- which in R currently (and oddly) may copy the whole data.table. Use set* syntax instead to avoid copying: ?set, ?setnames and ?setattr. If this message doesn't help, please report your use case to the data.table issue tracker so the root cause can be fixed or this message improved.
> iris[,gsd:=character()]
Error in assign(as.character(name), x, parent.frame(), inherits = TRUE) : 
  cannot change value of locked binding for 'iris'
In addition: Warning message:
In `[.data.table`(iris, , `:=`(gsd, character())) :
  Invalid .internal.selfref detected and fixed by taking a (shallow) copy of the data.table so that := can add this new column by reference. At an earlier point, this data.table has been copied by R (or was created manually using structure() or similar). Avoid key<-, names<- and attr<- which in R currently (and oddly) may copy the whole data.table. Use set* syntax instead to avoid copying: ?set, ?setnames and ?setattr. If this message doesn't help, please report your use case to the data.table issue tracker so the root cause can be fixed or this message improved.
> iris[,':='(vik = character())]
Error in assign(as.character(name), x, parent.frame(), inherits = TRUE) : 
  cannot change value of locked binding for 'iris'
In addition: Warning message:
In `[.data.table`(iris, , `:=`(vik = character())) :
  Invalid .internal.selfref detected and fixed by taking a (shallow) copy of the data.table so that := can add this new column by reference. At an earlier point, this data.table has been copied by R (or was created manually using structure() or similar). Avoid key<-, names<- and attr<- which in R currently (and oddly) may copy the whole data.table. Use set* syntax instead to avoid copying: ?set, ?setnames and ?setattr. If this message doesn't help, please report your use case to the data.table issue tracker so the root cause can be fixed or this message improved.

Please help if there is a new way to add an empty or normal column in a data.table

This is telling you that iris is locked.

If you use your own data, you should be ok, e.g. try this:

iris %>% setDT()
iris_copy <- copy(iris)

4 Likes

Package namespaces are locked, meaning the objects inside them can't be changed once they're loaded. Normally, R copies items to "edit" them, so changing package datasets doesn't have a problem: the dataset is copied, changed, and the new one masks the old.

address(iris)
# [1] "00000000141A7FB0"

## After adding a column, we're actually using a different iris dataset
iris[["f"]] <- 1
address(iris)
# [1] "0000000013E6F648"

However, data.table edits objects without copying them.

library(data.table)
library(magrittr)

address(iris)
# [1] "000000000A21D608"

## iris is still the dataset inside the locked namespace
iris %>% setDT()
address(iris)
# [1] "000000000A21D608"

Note that the data() function creates a copy of a dataset. So, if you use that first, you should be fine. In any case, it's the recommended way to lazy-load datasets from packages.

library(data.table)

address(iris)
# [1] "0000000008F70980"

data("iris")
address(iris)
# [1] "0000000008F44818"
4 Likes

@martin.R and @nwerth thanks for replying on the question. I had same kind of behavior on a current dataset as well.

It was just for reference I had this error in one of my shiny app. Where on adding the column same way it gave me.

Internal self reference error and something about creating a copy of the dataset. And it doesn't recycle values anymore and so on...

All I want to know is how would you add a column now in data.table. If you can just tell me that I would be fine.

Thanks in advance.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.