Skip to content

rowwise() and empty input #7039

@krlmlr

Description

@krlmlr

Should we guarantee that the functions used in a rowwise mutate() or summarize() or reframe() always see length-one input? This is currently the case for everything but empty input, which creates a corner case to be worked around in user code. The example below shows mutate(), but the behavior is very similar for summarize() and reframe()

options(conflicts.policy = list(warn = FALSE))

library(dplyr)

tibble(a = 1L) |>
  rowwise() |>
  mutate(print(a))
#> [1] 1
#> # A tibble: 1 × 2
#> # Rowwise: 
#>       a `print(a)`
#>   <int>      <int>
#> 1     1          1
tibble(a = 1:3) |>
  rowwise() |>
  mutate(print(a))
#> [1] 1
#> [1] 2
#> [1] 3
#> # A tibble: 3 × 2
#> # Rowwise: 
#>       a `print(a)`
#>   <int>      <int>
#> 1     1          1
#> 2     2          2
#> 3     3          3
# Should this print `NA_integer_` ?
tibble(a = integer()) |>
  rowwise() |>
  mutate(print(a))
#> integer(0)
#> # A tibble: 0 × 2
#> # Rowwise: 
#> # ℹ 2 variables: a <int>, print(a) <int>

Created on 2024-06-12 with reprex v2.1.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