I've posted this to the https://community.rstudio.com/c/tidyverse discussion in answer to a question I had asked about why add_column could not add a column to a tibble that had 0 columns (tibble())
I've come to the conclusion that add_column is producing an undocumented and unexpected result when .data (the input tibble) has 0 columns.
Here is why I think this...
I checked the source for add_column and it checks for a number of edge cases, for example where the number of added columns is 0, but the case where .data (i.e. the input tibble) has 0 columns is not checked.
This produces a the confusing error "Error: 0 is not TRUE", which I would call a programming surprise because does not look to be intentional :-) .
This happens because later in the code pluralise_n is used with nrow(.data), i.e. pluralise_n is used with 0,.to make an error message. pluralise_n produces the low level message ""Error: 0 is not TRUE" when it is asked to pluralize 0.
However, after a bit of checking and setup, all that add_column does is concatenate the added columns with the ones already in the tibble. This make sense because a tibble stores it's content as a sequence of columns.
So... it would make sense (IMHO of course) for add_column to check to see if the .data (the input tibble) contained 0 columns and if it did just return the input columns (i.e ...) as a new tibble. I think that this kind of behavior would be more consistent with name and implied behavior of a function named add_column.
Supporting to a 0 column .data could be added with a simple test at the beginning of the code.
#existing code
if (ncol(df) == 0L) {
return(.data)
}
#add this to support 0 columns .data
if(ncol(.data)==0L) {
return(df)
}
I've posted this to the https://community.rstudio.com/c/tidyverse discussion in answer to a question I had asked about why add_column could not add a column to a tibble that had 0 columns (tibble())
I've come to the conclusion that add_column is producing an undocumented and unexpected result when .data (the input tibble) has 0 columns.
Here is why I think this...
I checked the source for add_column and it checks for a number of edge cases, for example where the number of added columns is 0, but the case where .data (i.e. the input tibble) has 0 columns is not checked.
This produces a the confusing error "Error: 0 is not TRUE", which I would call a programming surprise because does not look to be intentional :-) .
This happens because later in the code pluralise_n is used with nrow(.data), i.e. pluralise_n is used with 0,.to make an error message. pluralise_n produces the low level message ""Error: 0 is not TRUE" when it is asked to pluralize 0.
However, after a bit of checking and setup, all that add_column does is concatenate the added columns with the ones already in the tibble. This make sense because a tibble stores it's content as a sequence of columns.
So... it would make sense (IMHO of course) for add_column to check to see if the .data (the input tibble) contained 0 columns and if it did just return the input columns (i.e ...) as a new tibble. I think that this kind of behavior would be more consistent with name and implied behavior of a function named add_column.
Supporting to a 0 column .data could be added with a simple test at the beginning of the code.
#existing code
if (ncol(df) == 0L) {
return(.data)
}
#add this to support 0 columns .data
if(ncol(.data)==0L) {
return(df)
}