Non standard evaluation of fun.aggregate argument can identify the wrong user supplied function

Open
#2,449 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start with the reproducible dcast example and inspect how fun.aggregate resolves supplied functions when a user-defined sum shadows a package function. Done means dcast identifies and applies the user-supplied function correctly, while preserving the expected aggregation output.

Written by the indexing model from the issue text.

Description

reshape

# Minimal reproducible example

# example data.table
z = as.data.table(expand.grid(a=LETTERS[1:3],b=1:3,c=5:6,d=3:4,stringsAsFactors =FALSE))[sample(36,9)]

# "accidentally" name your function with the same name as function in the
# scope of the `data.table` package
sum  <-  function(x){2}

# get unexpected results when attempting to pass your custom function to
# `dcast`'s fun.aggregate
dcast(z,
      a~.,
      fun=list(sum,function(x){2}),
      value.var = list('c','c'))

>     a c_sum c_function
>  1: A    28          2
>  2: B    22          2

# Output of sessionInfo()

Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.4

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] data.table_1.10.4-2

loaded via a namespace (and not attached):
[1] compiler_3.4.0 tools_3.4.0

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.