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

Can pass quosure to col arg in separate() #360

Merged
merged 5 commits into from
Jun 17, 2022
Merged

Conversation

markfairbanks
Copy link
Collaborator

Closes #359

@markfairbanks markfairbanks requested a review from eutwt June 17, 2022 16:18
Copy link
Collaborator

@eutwt eutwt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@eutwt
Copy link
Collaborator

eutwt commented Jun 17, 2022

This fixes the linked issue and doesn't introduce any new bugs. But, when looking through this I realized separate with column numbers doesn't work in dtplyr. Would be fine to merge this and address that issue in a different PR if you want though, I think.

library(tidyr, warn.conflicts = FALSE)
library(data.table, warn.conflicts = FALSE)
library(dtplyr)

dt <- data.table(x = c("a_a", "b_b", "c_c"))
df <- as.data.frame(dt)

df %>% 
  separate(1, into = c("left", "right"))
#>   left right
#> 1    a     a
#> 2    b     b
#> 3    c     c
dt %>% 
  separate(1, into = c("left", "right"))
#> Warning: Using `as.character()` on a quosure is deprecated as of rlang 0.3.0.
#> Please use `as_label()` or `as_name()` instead.
#> This warning is displayed once per session.
#> Source: local data table [3 x 2]
#> Call:   copy(`_DT1`)[, `:=`(c("left", "right"), tstrsplit(~1, split = "[^[:alnum:]]+"))][, 
#>     .(left, right)]
#> 
#>   left  right
#>   <chr> <chr>
#> 1 1     1    
#> 2 1     1    
#> 3 1     1    
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results

Created on 2022-06-17 by the reprex package (v2.0.1)

R/step-subset-separate.R Outdated Show resolved Hide resolved
tests/testthat/test-step-subset-separate.R Show resolved Hide resolved
@markfairbanks
Copy link
Collaborator Author

@eutwt I ended up using tidyselect::eval_select() to allow for numeric selection. I also added your recommended test, but I left out the snapshot since the error message is taken care of by tidyselect.

@markfairbanks markfairbanks merged commit 8264804 into main Jun 17, 2022
@markfairbanks markfairbanks deleted the separate-col-quosure branch June 17, 2022 19:55
@eutwt
Copy link
Collaborator

eutwt commented Jun 17, 2022

@markfairbanks I think it should error on >1 column

library(tidyr, warn.conflicts = FALSE)
library(data.table, warn.conflicts = FALSE)
devtools::load_all("/Users/mbp/Documents/GitHub/dtp")
#> ℹ Loading dtplyr
#> Warning: package 'dplyr' was built under R version 4.1.2

dt <- data.table(x = c("a_a", "b_b", "c_c"))[, y := x]
df <- as.data.frame(dt)

df %>% 
  separate(c(x, y), into = c("left", "right")) 
#> Error in `pull_as_location2()`:
#> ! Must extract column with a single valid subscript.
#> ✖ Subscript `var` has size 2 but must be size 1.
dt %>% 
  separate(c(x, y), into = c("left", "right")) 
#> Source: local data table [3 x 3]
#> Call:   copy(`_DT1`)[, `:=`(c("left", "right"), tstrsplit(x, split = "[^[:alnum:]]+"))][, 
#>     .(y, left, right)]
#> 
#>   y     left  right
#>   <chr> <chr> <chr>
#> 1 a_a   a     a    
#> 2 b_b   b     b    
#> 3 c_c   c     c    
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results

Created on 2022-06-17 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

separate() fails when the input is a data.table instead of a lazy_dt
2 participants