Tidyr: Define a default value of the cols argument of pivot_longer

Created on 1 Jul 2019  路  4Comments  路  Source: tidyverse/tidyr

pivot_longer's cols argument currently misses a default argument.
What about everything() because

  • it is consistent to what gather does.
  • it avoids RStudio saying "argument 'cols' is missing, with no default" when using manual spec.

image

feature pivoting

Most helpful comment

@DavisVaughan yeah, I think your analysis is correct. I don't think I like those function names, but given that you hardly ever need to use spec any more, I think it makes sense to pull the spec functions out.

All 4 comments

Another possibility is cols = NULL as a default value, and returning an error if cols nor spec are specified by user.

iris %>% pivot_longer(NULL)
#> Error in `[[<-.data.frame`(`*tmp*`, ".value", value = "value") :  replacement has 1 row, data has 0

I like that option better; it's more inline with https://principles.tidyverse.org/def-required.html

I actually think it's the other way around after reading the principles. Specifically the mutually exclusive section https://principles.tidyverse.org/def-required.html#args-mutually-exclusive

It seems like both cols and spec should have no default, because you must supply one or the other (an error should be thrown if you supply both).

However this is made more complex by the fact that the 6 names_* and values_* arguments that get passed on to pivot_longer_spec() should also not be allowed if spec is provided. That sounds like this case from the principles: "This technique should only be used for are exactly two possible arguments. If there are more than two , that is generally a sign you should create more functions."

My thought is to have an interface of:

pivot_longer()
pivot_longer_spec() # manually take the spec
pivot_wider()
pivot_wider_spec()
spec_longer() # the old pivot_longer_spec()
spec_wider()
pivot_longer <- function(data,
                         cols,
                         names_to = "name",
                         names_prefix = NULL,
                         names_sep = NULL,
                         names_pattern = NULL,
                         names_ptypes = list(),
                         values_to = "value",
                         values_drop_na = FALSE,
                         values_ptypes = list())

pivot_longer_spec <- function(data,
                              spec,
                              values_drop_na = FALSE,
                              values_ptypes = list())

# the current pivot_longer_spec() becomes:
spec_longer()
pivot_wider <- function(data,
                        id_cols = NULL,
                        names_from = name,
                        names_prefix = "",
                        names_sep = "_",
                        values_from = value,
                        values_fill = NULL,
                        values_fn = NULL)

pivot_wider_spec <- function(data,
                             spec,
                             id_cols = NULL,
                             values_fill = NULL,
                             values_fn = NULL)

# the current pivot_wider_spec() becomes
spec_wider()

I find it very clear to understand what arguments are still available to tweak if you supply a spec

I also like that the spec argument can become spec = spec_longer(). That naming convention sounds nice to me.

This also has the benefit that pivot_longer() and pivot_wider() can standalone and have their own help pages, and then have a note that says "if you need finer control, build your own specification with spec_longer() and supply it to pivot_longer_spec()" which would be together on a different help page.

_At the very least the spec argument of pivot_longer() and pivot_wider() probably need to be updated to mention the other columns that you can't use if you provide a spec._

@DavisVaughan yeah, I think your analysis is correct. I don't think I like those function names, but given that you hardly ever need to use spec any more, I think it makes sense to pull the spec functions out.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidhunterwalsh picture davidhunterwalsh  路  4Comments

pachadotdev picture pachadotdev  路  3Comments

mindymallory picture mindymallory  路  3Comments

coatless picture coatless  路  6Comments

kendonB picture kendonB  路  5Comments