Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9284209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:50:40+00:00 2026-06-18T18:50:40+00:00

The package data.table has some special syntax that requires one to use expressions as

  • 0

The package data.table has some special syntax that requires one to use expressions as the i and j arguments.

This has some implications for how one write functions that accept and pass arguments to data tables, as is explained really well in section 1.16 of the FAQs.

But I can’t figure out how to take this one additional level.

Here is an example. Say I want to write a wrapper function foo() that makes a specific summary of my data, and then a second wrapper plotfoo() that calls foo() and plots the result:

library(data.table)


foo <- function(data, by){
  by <- substitute(by)
  data[, .N, by=list(eval(by))]
}

DT <- data.table(mtcars)
foo(DT, gear)

OK, this works, because I get my tabulated results:

   by  N
1:  4 12
2:  3 15
3:  5  5

Now, I try to just the same when writing plotfoo() but I fail miserably:

plotfoo <- function(data, by){
  by <- substitute(by)
  foo(data, eval(by))
}
plotfoo(DT, gear)

But this time I get an error message:

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?

OK, so the eval() is causing a problem. Let’s remove it:

plotfoo <- function(data, by){
  by <- substitute(by)
  foo(data, by)
}
plotfoo(DT, gear)

Oh no, I get a new error message:

Error in `[.data.table`(data, , .N, by = list(eval(by))) : 
  column or expression 1 of 'by' or 'keyby' is type symbol. Do not quote column names. Useage: DT[,sum(colC),by=list(colA,month(colB))]

And here is where I remain stuck.

Question: How to write a function that calls a function that calls data.table?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:50:41+00:00Added an answer on June 18, 2026 at 6:50 pm

    This will work:

    plotfoo <- function(data, by) {
      by <- substitute(by)
      do.call(foo, list(quote(data), by))
    }
    
    plotfoo(DT, gear)
    #    by  N
    # 1:  4 12
    # 2:  3 15
    # 3:  5  5
    

    Explanation:

    The problem is that your call to foo() in plotfoo() looks like one of the following:

    foo(data, eval(by))
    foo(data, by)
    

    When foo processes those calls, it dutifully substitutes for the second formal argument (by) getting as by‘s value the symbols eval(by) or by. But you want by‘s value to be gear, as in the call foo(data, gear).

    do.call() solves this problem by evaluating the elements of its second argument before constructing the call that it then evaluates. As a result, when you pass it by, it evaluates it to its value (the symbol gear) before constructing a call that looks (essentially) like this:

    foo(data, gear)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The data.table package is very helpful in terms of speed. But I am having
The data.table package provides many of the same table handling methods as SQL. If
R's data.table package offers fast subsetting of values based on keys. So, for example:
I am having a hard time trying to subset a data.table (package) in R.
i want copy pdf file from application path(/data/data/package name) to sdcard.for that i have
I found out during the debug that my db is in: /data/data/package-name/databases/... how can
In Android, assuming that I have files in /data/data/package.name/, without knowing the names or
We have an ETL package that writes data to the SQL Server. Data is
I have an SSIS package that queries data from a view using an SQL
I'm working with an SSIS Package that pulls data from a DB2 source, runs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.