How to identify if the user provides no arguments to dots?

I would like to find a way to identify if the dots are not used, so that I can make a function that provides different default behaviour where the dots are not used.

Below is an example to illustrate the problem. How could I identify within the function if the user has not provided any arguments to ... and if so instead just return all columns of the dataset instead of nothing?

library(dplyr)

select2 <- function(data, ...) {
  data %>% 
    select(...)
}

select2(iris)

Here is a suggestion (I have a feeling that there is a more elegant solution):

library(dplyr)

select2 <- function(data, ...) {
    tryCatch(expr = {
        str(...) #Do something that fails if function is called with no ...-arguments
        data%>% 
            select(...)
    },
    error=function(e){
        data
    }  )

}
select2(iris)

/Tbendsen

1 Like

Alternative solution:

library(dplyr)

select2 <- function(data, ...) {
    selectedData<-data%>% 
            select(...)
    if (length(names(selectedData))==0) {
        return(data)
    }
    return(selectedData)

}
select2(iris)

/tbendsen

1 Like
library(dplyr)

select2 <- function(data, ...) {
  if(...length()==0){
    stop("nada")
  }
  data %>% 
    select(...)
}

select2(iris)
1 Like

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.