nathaneastwood / poorman

A poor man's dependency free grammar of data manipulation
https://nathaneastwood.github.io/poorman/
Other
340 stars 15 forks source link

`.keep = "used"` misses variables used inside functions #87

Closed markfairbanks closed 3 years ago

markfairbanks commented 3 years ago
library(poorman)

test_df <- data.frame(x = 1:3, y = 1:3)

test_df %>%
  mutate(if_x = ifelse(x < 3, 1, 0), .keep = "used")
#>   if_x
#> 1    1
#> 2    1
#> 3    0

Comparison to dplyr:

library(dplyr)

test_df <- data.frame(x = 1:3, y = 1:3)

test_df %>%
  mutate(if_x = ifelse(x < 3, 1, 0), .keep = "used")
#>   x if_x
#> 1 1    1
#> 2 2    1
#> 3 3    0
nathaneastwood commented 3 years ago

Ah, this is a very nice find. Thanks a lot.

markfairbanks commented 3 years ago

This seems to work:

get_used <- function(x) {
  if (is.symbol(x)) {
    as.character(x)
  } else {
    unique(unlist(lapply(x[-1], get_used)))
  }
}

test_df <- data.frame(x = 1:3, y = 1:3, z = 1:3)
var <- 1

# test_expr <- quote(1 * 2) # Returns NULL
test_expr <- quote(x * var + ifelse(x < y, 1, 0) + n())

used <- get_used(test_expr)
used[used %in% names(test_df)]
#> [1] "x" "y"

There would also need to be some sort of if statement for the case where no vars were used and it returns NULL