Skip to content

Arithmetic operator / not causing an error in eval_select() #375

@rubenmartinez9

Description

@rubenmartinez9

The tidyselect vignette (https://tidyselect.r-lib.org/articles/syntax.html#arithmetic-operators) says:

Within data-expressions (see Evaluation section), +, * and / are overridden to cause an error.

The operators ^ and * do output my expected behavior:

mtcars %>% tidyselect:::select_loc(cyl^2)
#> Error in `tidyselect:::select_loc()`:
#> ! Can't use arithmetic operator `^` in selection context.

mtcars %>% tidyselect:::select_loc(cyl*2)
#> Error in `tidyselect:::select_loc()`:
#> ! Can't use arithmetic operator `*` in selection context.

However, that is not the case for the / operator. Here are a few examples of inconsistent outputs, none of which being ! Can't use arithmetic operator / in selection context.

# EXAMPLE 1 -- error related to something other than the `/` operator
mtcars %>% tidyselect:::select_loc(cyl/20)
#> Error in `tidyselect:::select_loc()`:
#> ! Can't select columns past the end.
#> ℹ Location 20 doesn't exist.
#> ℹ There are only 11 columns.

# EXAMPLE 2 -- no error; returns empty location
mtcars %>% tidyselect:::select_loc(cyl/2)
#> named integer(0)

# EXAMPLE 3 -- no error; returns location
mtcars %>% tidyselect:::select_loc(hp/2)
#> hp 
#> 4

While select isn't intended to perform any operations, the last example is particularly dangerous for someone originally using select and then updating the arguments to perform an arithmetic operation while forgetting to change to mutate.

# unexpected result
mtcars %>% 
  mutate(expected_hp_by_2 = hp/2) %>%
  select(expected_hp_by_2, hp_by_2 = hp/2) # user confuses mutate for select

I would advice emitting an error for all unsupported operators, in order to make it consistent with the current behavior of other operators.

PS: probably for another issue, but using the + operator in select throws a different error than ^ or *.

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