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

summarize_all() throws error when using tilde notation #134

Closed
krlmlr opened this issue Jul 25, 2018 · 3 comments

Comments

@krlmlr
Copy link
Member

@krlmlr krlmlr commented Jul 25, 2018

I'm not sure if this will be fixed with tidyverse/dplyr#3433. Also, I don't understand why the second example gives a warning. Thanks @romanik!

library(tidyverse)
dbplyr::memdb_frame(a = 1, b = 2) %>% 
  summarize_all(~mean(.))
#> Error in mean(.): object 'a' not found
dbplyr::memdb_frame(a = 1, b = 2) %>% 
  summarize_all(mean)
#> Warning: Missing values are always removed in SQL.
#> Use `AVG(x, na.rm = TRUE)` to silence this warning

#> Warning: Missing values are always removed in SQL.
#> Use `AVG(x, na.rm = TRUE)` to silence this warning
#> # Source:   lazy query [?? x 2]
#> # Database: sqlite 3.22.0 [:memory:]
#>       a     b
#>   <dbl> <dbl>
#> 1     1     2
dbplyr::memdb_frame(a = 1, b = 2) %>% 
  summarize_if(funs(is.numeric), ~mean(.))
#> Applying predicate on the first 100 rows
#> Error in mean(.): object 'a' not found

Created on 2018-07-25 by the reprex package (v0.2.0).

@karldw

This comment was marked as outdated.

Copy link
Contributor

@karldw karldw commented Nov 17, 2018

Lists of functions seem to have the same problem, and I get the same error for all of the summarize_*, mutate_*, and transmute_* functions.

library(tidyverse)

fn_list <- list(
  p=function(x) x/2,
  q=function(x) x/4
)
df <- dbplyr::memdb_frame(a=1, b=2) 

df %>%
  collect() %>% 
  transmute_at(vars(a,b), .funs=fn_list)
#> A tibble: 1 x 4
#>    a_p   b_p   a_q   b_q
#>  <dbl> <dbl> <dbl> <dbl>
#> 1  0.5     1  0.25   0.5

df %>% transmute_at(vars(a,b), .funs=fn_list)
#> Error in (function (x)  : object 'a' not found
@hadley

This comment has been minimized.

Copy link
Member

@hadley hadley commented Jan 2, 2019

Mild reprex tweaking

library(dplyr, warn.conflicts = FALSE)
mf <- dbplyr::memdb_frame(a = 1, b = 2)

# WORKS
mf %>% summarize_all(mean, na.rm = TRUE)
#> # Source:   lazy query [?? x 2]
#> # Database: sqlite 3.22.0 [:memory:]
#>       a     b
#>   <dbl> <dbl>
#> 1     1     2

# FAILS
mf %>% summarize_all(~mean(.))
#> Error in mean(.): object 'a' not found
mf %>% summarize_all(list(mean))
#> Error in (function (x, ...) : object 'a' not found

Created on 2019-01-02 by the reprex package (v0.2.1)

@hadley hadley added this to the v1.4.0 milestone Jan 9, 2019
@hadley

This comment has been minimized.

Copy link
Member

@hadley hadley commented Feb 6, 2019

summarise_all() creates a list of inlined functions so an even more focussed reprex is this:

library(dbplyr)

partial_eval(rlang::expr(mean(x)), vars = "x")
#> mean(x)
partial_eval(rlang::expr((!!mean)(x)), vars = "x")
#> Error in (function (x, ...) : object 'x' not found

Created on 2019-02-06 by the reprex package (v0.2.1.9000)

@hadley hadley closed this in 7af1929 Feb 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.