Skip to content

Alternative way to dynamic symbol usage in j #2884

Description

@renkun-ken

Using dynamically determined symbol in j can be quite annoying (as discussed in #633, #2589).

I'm not sure if the following idea makes sense, which seems easier to implement and nicer to look at for me.

library(data.table)
dt <- data.table(id = rep(1:2, 5), x1 = rnorm(10), x2 = rnorm(10), y1 = rnorm(10), y2 = rnorm(10))
dt[, .(xsum = sum(x), ysum = sum(y)), by = id, alias = c(x = "x1", y = "y2")]

In the above example, if a new argument like alias is introduced, a named character vector would give fixed names to dynamically determined symbols, which makes it much easier to use them in j.

Some new problems may occur such as when x or y are already defined in dt. But the syntax looks much cleaner if the dynamic symbols can be determined outside dt.

Activity

  1. jangorecki commented on May 16, 2018

    @jangorecki
    Member

    I like the idea, somehow similar to substitute() where env argument refers to alias. We could also substitute function names this way.

  2. jangorecki commented on Mar 22, 2020

    @jangorecki
    Member

    It seems that my PR #4304 actually re-invents and implements what @renkun-ken wrote in this issue, + maybe call arg names substitution :)

  3. renkun-ken commented on Mar 22, 2020

    @renkun-ken
    MemberAuthor

    Didn't remember this issue. Explains why your nice solution looks so great to me. 😄

  4. linked a pull request that will close this issueprogramming on data.table #4304on Jun 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    programmingparameterizing queries: get, mget, eval, env

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions