Skip to content

Instantly share code, notes, and snippets.

@alekrutkowski
Last active March 13, 2024 13:59
Show Gist options
  • Save alekrutkowski/ff5446fa709d42d50c5e975123c1bf40 to your computer and use it in GitHub Desktop.
Save alekrutkowski/ff5446fa709d42d50c5e975123c1bf40 to your computer and use it in GitHub Desktop.
R function for dealing with (ignoring) missing/empty arguments inside ellipsis (...)
listWithoutEmptyArgs <- function(...)
eval(Filter(\(x) !identical(as.character(x), "") || identical(x,""),
bquote(.(substitute(list(...))))))
# > list(a=1,,b=2:10)
# Error in list(a = 1, , b = 2:10) : argument 2 is empty
# > listWithoutEmptyArgs(a=1,,b=2:10)
# list(a = 1, b = 2:10)
@alekrutkowski
Copy link
Author

N.B. list is the most universal/general native structure in R, which you can convert to more specialized data structures with e.g. as.data.frame or as.data.table or use for calling function with do.call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment