Skip to content

export shallow copy #2323

@franknarf1

Description

@franknarf1

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.

library(data.table)

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).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions