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

Splice data.frame inputs to mutate, summarise, and transmute #342

Open
eutwt opened this issue Feb 11, 2022 · 2 comments
Open

Splice data.frame inputs to mutate, summarise, and transmute #342

eutwt opened this issue Feb 11, 2022 · 2 comments
Labels
feature a feature request or enhancement

Comments

@eutwt
Copy link
Collaborator

eutwt commented Feb 11, 2022

library(dplyr, warn.conflicts = FALSE)
library(dtplyr)

df <- tibble(x = 1)

df %>%
  mutate(data.frame(y = 2, z = 3))
#> # A tibble: 1 × 3
#>       x     y     z
#>   <dbl> <dbl> <dbl>
#> 1     1     2     3

lazy_dt(df) %>%
  mutate(data.frame(y = 2, z = 3))
#> Source: local data table [1 x 2]
#> Call:   copy(`_DT1`)[, `:=`(`data.frame(y = 2, z = 3)` = data.frame(y = 2, 
#>     z = 3))]
#> 
#>       x `data.frame(y = 2, z = 3)`
#>   <dbl> <list>                    
#> 1     1 <df [1 × 2]>              
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results

Created on 2022-02-11 by the reprex package (v2.0.1)

@markfairbanks
Copy link
Collaborator

markfairbanks commented Jun 17, 2022

Is this going to be possible in a lazy workflow if passed a data frame object from the environment?

Edit: As I think about this, all we would have to do is use exists()/env_has() and then check if an input is a data.frame.

library(dplyr, warn.conflicts = FALSE)

df <- tibble(x = 1)

new_cols_df <- data.frame(y = 2, z = 3)

df %>%
  mutate(new_cols_df)
#> # A tibble: 1 × 3
#>       x     y     z
#>   <dbl> <dbl> <dbl>
#> 1     1     2     3

@eutwt
Copy link
Collaborator Author

eutwt commented Jun 17, 2022

When I wrote the issue I was thinking about just checking for a data.frame/tibble/as.data.frame/as_tibble call, to allow for complex calculations inside brackets in mutate. I can't remember the example I ran into at the time where it's actually necessary/useful right now, but something like below. This is a pretty rare thing to do though, and if it is possible to reliably splice data frames not coming from an explicit in-verb call that'd be even better.

mtcars %>% 
  #lazy_dt() %>% 
  mutate({
    a <- mpg[1]
    b <- sqrt(a) + hp[4]
    data.frame(a, b)
  })

@markfairbanks markfairbanks added the feature a feature request or enhancement label Jun 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants