Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NULL vs zero-length vectors #24

Open
hadley opened this issue Oct 10, 2018 · 5 comments

Comments

@hadley
Copy link
Member

commented Oct 10, 2018

For vectorised arguments I think we should consistently treat NULL in the same way as if the argument had not been supplied. This is symmetric with our use of NULL for the default value of optional arguments that need complex calculations.

library(vctrs)
vec_c(TRUE, double())
#> [1] 1

vec_c(TRUE, NULL)
#> [1] TRUE
# Same as 
vec_c(TRUE, )
#> [1] TRUE
# Same as
vec_c(TRUE)
#> [1] TRUE
@hadley

This comment has been minimized.

Copy link
Member Author

commented Oct 10, 2018

For glue this would imply that the following calls should be identical:

glue::glue("{a}: {b}", a = 1:2)
#> Error in eval(parse(text = text, keep.source = FALSE), envir): object 'b' not found
glue::glue("{a}: {b}", a = 1:2, b = )
#> Error in eval(parse(text = text, keep.source = FALSE), envir): argument is missing, with no default
glue::glue("{a}: {b}", a = 1:2, b = NULL)

And for purrr:

str(purrr::pmap(rlang::list2(1, 2), list))
#> List of 1
#>  $ :List of 2
#>   ..$ : num 1
#>   ..$ : num 2
str(purrr::pmap(rlang::list2(1, 2, ), list))
#> List of 1
#>  $ :List of 2
#>   ..$ : num 1
#>   ..$ : num 2
str(purrr::pmap(rlang::list2(1, 2, NULL), list))
#>  list()

For tibble:

tibble::tibble(x = 1:2)
#> # A tibble: 2 x 1
#>       x
#>   <int>
#> 1     1
#> 2     2
tibble::tibble(x = 1:2, y = NULL)
#> Error: Column `y` must be a 1d atomic vector or a list

# vs
tibble::tibble(x = 1:2, y = integer())
#> Error: Column `y` must be length 1 or 2, not 0
@hadley

This comment has been minimized.

Copy link
Member Author

commented Nov 28, 2018

Also comes up in pluck()

@hadley

This comment has been minimized.

Copy link
Member Author

commented Feb 19, 2019

Connected to #13

@krlmlr

This comment has been minimized.

Copy link
Member

commented Feb 22, 2019

Is the current behavior of mutate(iris, Petal.Width = NULL) consistent with that description? (I admit I sometimes use it to say "I don't care if this column exists or not, just get rid of it", but it doesn't seem completely right.)

@hadley

This comment has been minimized.

Copy link
Member Author

commented May 28, 2019

@krlmlr I think that's a special case where it's ok to have different behaviour.

And now implemented in tidyr::expand_grid(). This ensures that for vector inputs, vec_size(expand_grid(x1, x2, ..., xn)) = vec_size(x1) * vec_size(x2) * ... * vec_size(xn).

@hadley hadley referenced this issue Sep 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.