A few years ago there was no error, now it errors

Hi, this is from SO and once it was a good solution:
https://stackoverflow.com/questions/14290364/heatmap-with-values-ggplot2

library(tidyverse)

## make data
dat <- matrix(rnorm(100, 3, 1), ncol=10)

## reshape data (tidy/tall form)
dat2 <- dat %>%
    tbl_df() %>%
    rownames_to_column('Var1') %>%
    gather(Var2, value, -Var1) %>%
    mutate(
        Var1 = factor(Var1, levels=1:10),
        Var2 = factor(gsub("V", "", Var2), levels=1:10)
    )

I assume that worked in the past but now it throws an error:

Error:
! Columns 1, 2, 3, 4, 5, and 5 more must be named.
Use .name_repair to specify repair.
Caused by error in `repaired_names()`:
! Names can't be empty.
✖ Empty names found at locations 1, 2, 3, 4, 5, etc.

What should I do to fix this ?

Use as_tibble() instead of tbl_df()

library(tidyverse)

## make data
dat <- matrix(rnorm(100, 3, 1), ncol=10)

## reshape data (tidy/tall form)
dat %>%
    as_tibble() %>% 
    rownames_to_column('Var1') %>%
    gather(Var2, value, -Var1) %>%
    mutate(
        Var1 = factor(Var1, levels=1:10),
        Var2 = factor(gsub("V", "", Var2), levels=1:10)
    )
#> Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble 2.0.0.
#> Using compatibility `.name_repair`.
#> # A tibble: 100 × 3
#>    Var1  Var2  value
#>    <fct> <fct> <dbl>
#>  1 1     1      1.46
#>  2 2     1      1.97
#>  3 3     1      2.26
#>  4 4     1      4.80
#>  5 5     1      2.87
#>  6 6     1      2.13
#>  7 7     1      3.70
#>  8 8     1      2.15
#>  9 9     1      2.51
#> 10 10    1      3.89
#> # … with 90 more rows

Created on 2022-09-29 with reprex v2.0.2

Great, thank you, so actually there is no need to use .name_repair argument ? The error suggested to use it.

The default behavior is what is expected by the gsub() step, if you use a different .name_repair option you would need to adjust the regular expression accordingly.

Could you please show it to me on example, I would like to understand how to properly use .name_repair in the future.

In your example the source object, dat, is a matrix of 10 columns, there are no names as such.
Therefore creating a tibble would require defining some names...
the name_repair strategies are "check_unique", "unique","universal","minimal" and actually a NULL strategy - that is probably the one what you will find most convenient. Here is a short code to illustrate how they would create names based off of the dat input

library(tidyverse)

dat <- matrix(rnorm(100, 3, 1), ncol=10)


name_rep <- list(NULL,"check_unique", 
                "unique", "universal", "minimal")

safe_as_tibble <- purrr::safely(as_tibble,
                                otherwise = list(failed="Failed"))

names_list <- map(name_rep,
    ~dat %>% 
      safe_as_tibble(.name_repair = .x) %>% 
      pluck("result") %>% 
      names()) %>% 
  set_names(name_rep)

#### what are the names each .name_repair strategy came up with ?

names_list

check_unique is perhaps the odd one out in that it is more concerned with failing out if the input names are not unique rather than creating names

you can be explicit and define your own nameing convention for something like dat for example

colnames(dat) <- paste0("col_",seq_len(ncol(dat)))
as_tibble(dat)

Thank you both Andres and Nir.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.