Skip to content

names<-.tbl_df has different behavior inside vs outside RStudio #1662

@khusmann

Description

@khusmann

It looks like this is a leftover workaround in tibble from RStudio 1.1:

# workaround for RStudio v1.1, which relies on the ability to set

This results in significantly different behavior when running inside vs outside RStudio. Example:

tibble::tibble(x = 1, y = 2) |> unname() |> names()
#> NULL

Sys.unsetenv("RSTUDIO") # Make it look like we're running outside RStudio
tibble::tibble(x = 1, y = 2) |> unname() |> names()
#> [1] NA NA

For me, this caused some pretty unexpected behavior where I was getting different results when a test case was being run via testthat (which will run some tests in an env without RSTUDIO set) vs interactively... took quite a bit of effort to track down.

I'm assuming RStudio v1.1 isn't supported anymore -- is it still necessary in modern RStudio? Would you consider removing the workaround to avoid this brittle behavior? If not, maybe we could add a warning when the workaround is triggered?

Note that also the first case (with the workaround triggered) it creates an unprintable tibble:

tibble::tibble(x = 1, y = 2) |> unname() |> print()
#> Error in names[old] <- names(x)[j[old]] : replacement has length zero

Sys.unsetenv("RSTUDIO") # Make it look like we're running outside RStudio
tibble::tibble(x = 1, y = 2) |> unname() |> print()
#> # A tibble: 1 × 2
#>      ``    ``
#>   <dbl> <dbl>
#> 1     1     2

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