Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutate doesn't work with functions with format pkg::function #412

Closed
wch opened this issue Apr 30, 2014 · 6 comments
Closed

Mutate doesn't work with functions with format pkg::function #412

wch opened this issue Apr 30, 2014 · 6 comments
Assignees
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@wch
Copy link
Member

wch commented Apr 30, 2014

For example, with dplyr not attached:

dplyr::mutate(mtcars, cyl2 = dplyr::lag(cyl))
# Error in as.character(substitute(name)) : 
#   cannot coerce type 'closure' to vector of type 'character'

dplyr::mutate(mtcars, cyl2 = lag(cyl))
# OK
@smbache
Copy link
Member

smbache commented May 18, 2014

On the less serious side, one can do

mtcars %>% 
   (c(quote(dplyr::mutate), 
    cyl2 = substitute(f(cyl, 2), list(f = dplyr::lag))) %>% 
    as.call)

@hadley hadley added the bug label Aug 1, 2014
@hadley hadley added this to the 0.3.1 milestone Aug 1, 2014
@hadley hadley modified the milestones: 0.3.1, 0.3 Aug 1, 2014
@hadley
Copy link
Member

hadley commented Aug 1, 2014

@romainfrancois This is another hybrid evaluation buglet, I think. Probably need to hybrid implementation of ::.

@andrewblim
Copy link

Using R 3.1, dplyr 0.2, OS X 10.9.4: @wch's second call without the dplyr scoping does not work correctly for me, as the cyl2 column is the same as cyl:

> dplyr::mutate(mtcars, cyl2 = lag(cyl)) %>% dplyr::select(cyl, cyl2)
   cyl cyl2
1    6    6
2    6    6
3    4    4
4    6    6
5    8    8
...

I guess this is because it uses stats::lag and this doesn't work correctly? (is this a bug maybe, is this supposed to work?) However it seems I can force the use of dplyr::lag and get a correct result by surrounding the call in parens, a little easier than @smbache's solution:

> dplyr::mutate(mtcars, cyl2 = (dplyr::lag(cyl))) %>% dplyr::select(cyl, cyl2)
   cyl cyl2
1    6   NA
2    6    6
3    4    6
4    6    4
5    8    6
...

@renkun-ken
Copy link

stats::lag() works perfectly with pipeR:

library(pipeR)
mtcars %>>%
  dplyr::mutate(cyl2 = lag(cyl)) %>>%
  dplyr::select(cyl, cyl2) %>>%
  head
  cyl cyl2
1   6   NA
2   6    6
3   4    6
4   6    4
5   8    6
6   6    8

Does not work with dplyr::lag():

mtcars %>>%
  dplyr::mutate(cyl2 = dplyr::lag(cyl)) %>>%
  dplyr::select(cyl, cyl2) %>>%
  head
Error in as.character(substitute(name)) : 
  cannot coerce type 'closure' to vector of type 'character'

Does not find dplyr::lag in ():

mtcars %>>%
  dplyr::mutate(cyl2 = (dplyr::lag(cyl))) %>>%
  dplyr::select(cyl, cyl2) %>>%
  head
Error: 'lag' is not an exported object from 'namespace:dplyr'

@wch
Copy link
Member Author

wch commented Sep 15, 2014

@renkun-ken The problem is with calling stats::lag() (instead of lag()) in mutate(). (The dplyr::lag() function was recently removed in favor of stats::lag() but the problem remains.)

@renkun-ken
Copy link

Thanks @wch for your remind :) NSE problems always look frustrating...

@lock lock bot locked as resolved and limited conversation to collaborators Jun 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

6 participants