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

evaluate variables without curleys #426

Merged
merged 31 commits into from
Jun 3, 2023
Merged

Conversation

strengejacke
Copy link
Member

@strengejacke strengejacke commented May 27, 2023

@etiennebacher @DominiqueMakowski I have a proposal how to get rid of the nasty curley braces in data_filter() and keeping/maximizing the flexibility... WDYT?

library(datawizard)
data_filter(mtcars, cyl == 6)
#>                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Valiant        18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> Merc 280       19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#> Merc 280C      17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#> Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6

data_filter(mtcars, "cyl == 6")
#>                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Valiant        18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> Merc 280       19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#> Merc 280C      17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#> Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6

xx <- "cyl == 6"
data_filter(mtcars, xx)
#>                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Valiant        18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> Merc 280       19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#> Merc 280C      17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#> Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6

my_filter <- function(data, variable) {
  data_filter(data, variable)
}
my_filter(mtcars, "cyl == 6")
#>                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Valiant        18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> Merc 280       19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#> Merc 280C      17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#> Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6

my_filter2 <- function(data) {
  v <- "cyl == 6"
  data_filter(data, v)
}
my_filter2(mtcars)
#>                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Valiant        18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> Merc 280       19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#> Merc 280C      17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#> Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6


data_filter(mtcars, mpg <= 20, cyl == 6)
#>               mpg cyl  disp  hp drat   wt  qsec vs am gear carb
#> Valiant      18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
#> Merc 280     19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4
#> Merc 280C    17.8   6 167.6 123 3.92 3.44 18.90  1  0    4    4
#> Ferrari Dino 19.7   6 145.0 175 3.62 2.77 15.50  0  1    5    6

data_filter(mtcars, "mpg <= 20", "cyl == 6")
#>               mpg cyl  disp  hp drat   wt  qsec vs am gear carb
#> Valiant      18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
#> Merc 280     19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4
#> Merc 280C    17.8   6 167.6 123 3.92 3.44 18.90  1  0    4    4
#> Ferrari Dino 19.7   6 145.0 175 3.62 2.77 15.50  0  1    5    6

xx <- "cyl == 6"
yy <- "mpg <= 20"
data_filter(mtcars, xx, yy)
#>               mpg cyl  disp  hp drat   wt  qsec vs am gear carb
#> Valiant      18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
#> Merc 280     19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4
#> Merc 280C    17.8   6 167.6 123 3.92 3.44 18.90  1  0    4    4
#> Ferrari Dino 19.7   6 145.0 175 3.62 2.77 15.50  0  1    5    6

data_filter(mtcars, c("mpg <= 20", "cyl == 6"))
#>               mpg cyl  disp  hp drat   wt  qsec vs am gear carb
#> Valiant      18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
#> Merc 280     19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4
#> Merc 280C    17.8   6 167.6 123 3.92 3.44 18.90  1  0    4    4
#> Ferrari Dino 19.7   6 145.0 175 3.62 2.77 15.50  0  1    5    6


my_filter <- function(data, variable) {
  data_filter(data, variable)
}
my_filter(mtcars, c("mpg <= 20", "cyl == 6"))
#>               mpg cyl  disp  hp drat   wt  qsec vs am gear carb
#> Valiant      18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
#> Merc 280     19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4
#> Merc 280C    17.8   6 167.6 123 3.92 3.44 18.90  1  0    4    4
#> Ferrari Dino 19.7   6 145.0 175 3.62 2.77 15.50  0  1    5    6

my_filter2 <- function(data) {
  v <- c("mpg <= 20", "cyl == 6")
  data_filter(data, v)
}
my_filter2(mtcars)
#>               mpg cyl  disp  hp drat   wt  qsec vs am gear carb
#> Valiant      18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
#> Merc 280     19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4
#> Merc 280C    17.8   6 167.6 123 3.92 3.44 18.90  1  0    4    4
#> Ferrari Dino 19.7   6 145.0 175 3.62 2.77 15.50  0  1    5    6

my_filter3 <- function(data) {
  v1 <- "mpg <= 20"
  v2 <- "cyl == 6"
  data_filter(data, v1, v2)
}
my_filter3(mtcars)
#>               mpg cyl  disp  hp drat   wt  qsec vs am gear carb
#> Valiant      18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
#> Merc 280     19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4
#> Merc 280C    17.8   6 167.6 123 3.92 3.44 18.90  1  0    4    4
#> Ferrari Dino 19.7   6 145.0 175 3.62 2.77 15.50  0  1    5    6


data_filter(mtcars, 5:10)
#>                    mpg cyl  disp  hp drat   wt  qsec vs am gear carb
#> Hornet Sportabout 18.7   8 360.0 175 3.15 3.44 17.02  0  0    3    2
#> Valiant           18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
#> Duster 360        14.3   8 360.0 245 3.21 3.57 15.84  0  0    3    4
#> Merc 240D         24.4   4 146.7  62 3.69 3.19 20.00  1  0    4    2
#> Merc 230          22.8   4 140.8  95 3.92 3.15 22.90  1  0    4    2
#> Merc 280          19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4

data_filter(mtcars, "5:10")
#>                    mpg cyl  disp  hp drat   wt  qsec vs am gear carb
#> Hornet Sportabout 18.7   8 360.0 175 3.15 3.44 17.02  0  0    3    2
#> Valiant           18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
#> Duster 360        14.3   8 360.0 245 3.21 3.57 15.84  0  0    3    4
#> Merc 240D         24.4   4 146.7  62 3.69 3.19 20.00  1  0    4    2
#> Merc 230          22.8   4 140.8  95 3.92 3.15 22.90  1  0    4    2
#> Merc 280          19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4

xx <- "5:10"
data_filter(mtcars, xx)
#>                    mpg cyl  disp  hp drat   wt  qsec vs am gear carb
#> Hornet Sportabout 18.7   8 360.0 175 3.15 3.44 17.02  0  0    3    2
#> Valiant           18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
#> Duster 360        14.3   8 360.0 245 3.21 3.57 15.84  0  0    3    4
#> Merc 240D         24.4   4 146.7  62 3.69 3.19 20.00  1  0    4    2
#> Merc 230          22.8   4 140.8  95 3.92 3.15 22.90  1  0    4    2
#> Merc 280          19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4

Created on 2023-05-27 with reprex v2.0.2

@codecov-commenter

This comment was marked as outdated.

@strengejacke

This comment was marked as outdated.

@strengejacke
Copy link
Member Author

Ready for review, I think

@strengejacke
Copy link
Member Author

Here are two examples how code looked before, and what's the "new" way of writing the code with the changes made in this PR:

# old example
my_filter <- function(data, condition) {
  data_filter(data, {condition})
}
my_filter(mtcars, "am != 0")

# new example
my_filter <- function(data, condition) {
  data_filter(data, condition)
}
my_filter(mtcars, "am != 0")

# old example
my_filter <- function(data, variable) {
  data_filter(data, {variable} <= 20)
}
my_filter(mtcars, "mpg")

# new example
my_filter <- function(data, variable) {
  variable <- paste0(variable, " <= 20")
  data_filter(data, variable)
}
my_filter(mtcars, "mpg")

# or...
my_filter <- function(data, variable) {
  data_filter(data, variable)
}
my_filter(mtcars, "mpg <= 20")

@etiennebacher
Copy link
Member

I didn't have time to review yet but I suppose this solves #309?

@etiennebacher etiennebacher mentioned this pull request May 30, 2023
4 tasks
@strengejacke
Copy link
Member Author

Not sure, might be. It changes some of the behaviour in general, by replacing curley-braces evaluation with string-/character-variable evaluation (similar to data_modify() and alike).

Copy link
Member

@etiennebacher etiennebacher left a comment

Choose a reason for hiding this comment

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

Looks good to me, a bit hard to review with all the if conditions but the tests are quite complete

NEWS.md Outdated Show resolved Hide resolved
NEWS.md Outdated Show resolved Hide resolved
R/data_match.R Outdated Show resolved Hide resolved
R/data_match.R Outdated Show resolved Hide resolved
R/data_match.R Outdated Show resolved Hide resolved
strengejacke and others added 4 commits June 2, 2023 19:39
Co-authored-by: Etienne Bacher <[email protected]>
Co-authored-by: Etienne Bacher <[email protected]>
Co-authored-by: Etienne Bacher <[email protected]>
@strengejacke strengejacke merged commit 6d46974 into main Jun 3, 2023
@strengejacke strengejacke deleted the no_curly_in_data_find branch June 3, 2023 22:09
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.

3 participants