Skip to content

Name collision because of NSE with data.table #2

@SuperMayo

Description

@SuperMayo

Because the package makes extensive use of data.table, and because it uses extensively non-standard-evaluation(NSE). It may arrive that a column name is identical to some internal variable name, resulting in an error.

Example

library(didImputation)

data("did_simulated")
did_simulated$s <- rnorm(nrow(did_simulated))

didImputation(y ~ 0 | i + t,
              cohort = "g",
              data = did_simulated)

>> Error in s$time : $ operator is invalid for atomic vectors 

Explanation

The error comes from the following line

s$data[, k := get(s$time) - get(s$cohort)]

Indeed there is name collision between the internal object s of the package and the column name s.

Solution(s)

  1. Get rid of data.table NSE.
  2. Use quasiquotation.
  3. Rename sensible variables with weird names to have low collision probability 🤷‍♂️
  4. Use eval instead of get

In the meanwhile

Don't name a column s...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions