Skip to content

lead / lag within function with .data$ #441

Description

@shriv

I'm using dtplyr within an R package of data transformations. I'm using dtplyr_1.3.1 and dplyr_1.1.1 on an M1 mac.

I rely heavily on lead() and lag(). However, I find that neither lead() nor lag() can be used with columns referenced with .data$<column_name>. test_fn_working() has no issues but if you run test_fun_not_working() from the reprex, you get the following error.

Is there a workaround to use .data$<column_name> within lead() and lag() functions?

Error:
! Can't subset `.data` outside of a data mask context.
Run `rlang::last_trace()` to see where the error occurred.
library(dplyr)
library(dtplyr)

test_fn_working <- function(){
  
  dplyr::tibble(
    a = seq(1, 10, 1)
  ) |> 
    dtplyr::lazy_dt() |> 
    dplyr::mutate(
      b = lead(a)
    )
}

test_fn_not_working <- function(){
  
  dplyr::tibble(
    a = seq(1, 10, 1)
  ) |> 
    dtplyr::lazy_dt() |> 
    dplyr::mutate(
      b = lead(.data$a)
    )
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions