Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export shallow copy #2323

franknarf1 opened this issue Aug 28, 2017 · 1 comment

export shallow copy #2323

franknarf1 opened this issue Aug 28, 2017 · 1 comment


Copy link

@franknarf1 franknarf1 commented Aug 28, 2017

I'm posting in response to Matt's SO post and comments below.

I have a function, toopt(th, dat). The goal is to minimize the function in terms of its first argument. The second argument is a data.table. I guess this is a pretty common use case.


f = function(dat, th){
    dat[, prob := dpois(x, th) ][]

toopt = function(th, dat){
    mydat = data.table:::shallow(dat)
    f(mydat, th)
    mydat[, - sum(log(prob))]

nid = 10
nobs = 10
par = 20

DT = data.table(id = seq_len(nid))[, .(x = rpois(nobs, par)), by=id]
optimize(toopt, dat = DT, c(0,100))
mean(DT$x) # the closed-form solution

Inside optim or optimize, the function is called many times as it searches for the optimum.

Sorry if this example is too trivial or abstract. In my actual example, I have a sequence of functions like f that add columns (stored in separate functions so I can isolate them for easier testing and to reduce duplication, since I have multiple toopt functions that are similar but not identical); and my DT has some columns that I don't need for the calculations in toopt.

Besides functions passed to optim, I also use / want to use shallow in other functions, so I can benefit from the efficiency of not making a copy without worrying about whether my input will be messed with (if I didn't use copy) or about whether I need to to prune my input table's columns down to the minimum required for the function (if I did use copy).

Copy link
Contributor Author

@franknarf1 franknarf1 commented Jul 26, 2019

Besides dtplyr #3665, another recent example from SO:

So you want to create a new column in a data.table? Any reason this doesn't work: mtcarsDT[, newcol := myFunc()]? – Andrew 18 hours ago
Yes and No. I don't really want to create a column, I just want to view the result of the expression next to the existing columns without having to save the column and then delete it later – Dean MacGregor 18 hours ago

@jangorecki jangorecki changed the title [Request] export shallow or similar export shallow copy Jul 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants