Skip to content

Translation of dplyr::if_else() breaks if .data$var is used #220

@mrcaseb

Description

@mrcaseb

I am using dplyr inside a package and therefore do .data$var to avoid tidy evaluation R CMD check NOTEs

dtplyr translates this falsely inside a dplyr::if_else()

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

tib_lazy <- tibble::tibble(num = 1:5) %>% 
  dtplyr::lazy_dt() %>% 
  dplyr::mutate(
    # don't use .data$ -> works well
    lvl = dplyr::if_else(num <= 3, "low", "high"),
    # use .data$ in if_else -> the string ".data$" remains in the query
    lvl_.data = dplyr::if_else(.data$num <= 3, "low", "high"),
    # use .data$ in base ifelse -> works but doesn't call fifelse
    lvl_alt = ifelse(.data$num <= 3, "low", "high"),
    # use .data$ outside if_else works well
    num_alt = .data$num * 10
  )

# lvl_.data is NA
# note the string ".data$" in the fifelse query of the lvl_.data variable

tib_lazy
#> Source: local data table [5 x 5]
#> Call:   copy(`_DT1`)[, `:=`(lvl = fifelse(num <= 3, "low", "high"), lvl_.data = fifelse(.data$num <= 
#>     3, "low", "high"), lvl_alt = ifelse(num <= 3, "low", "high"), 
#>     num_alt = num * 10)]
#> 
#>     num lvl   lvl_.data lvl_alt num_alt
#>   <int> <chr> <chr>     <chr>     <dbl>
#> 1     1 low   <NA>      low          10
#> 2     2 low   <NA>      low          20
#> 3     3 low   <NA>      low          30
#> 4     4 high  <NA>      high         40
#> 5     5 high  <NA>      high         50
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results

Created on 2021-03-08 by the reprex package (v1.0.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions