Like traps for invalid or undefined arguments in R function calls when ellipsis is used in a definition

I am struggling with a problem that is recognized, but for which I still have to find an easy solution: how to catch the vague arguments passed into R-functions when the function definition includes an ellipsis or three points ....

As stated in Wickham Advanced R:

Use ...occurs at a price - any arguments with errors will not raise an error, and any arguments after ...should be fully named. This makes it easy to make typos invisible.

Here is an example:

myfun <- function(x, ...) 
    UseMethod("myfun")

myfun.character <- function(x, toLower = FALSE, ...)
    cat("Sent and transformed by myfun:", ifelse(toLower, tolower(x), x), "\n")

myfun("Test String with CaPiTaLs.")
## Sent and transformed by myfun: Test String with CaPiTaLs.
myfun("Test String with CaPiTaLs.", toLower = TRUE)
## Sent and transformed by myfun: test string with capitals. 
myfun("Test String with CaPiTaLs.", tolower = TRUE)
## Sent and transformed by myfun: Test String with CaPiTaLs.
myfun("Test String with CaPiTaLs.", toLower = TRUE, notDefined = 1)
## Sent and transformed by myfun: test string with capitals. 

toLower , , , . , , , , , - ....

, ... list(...), , , , , , ....

, , - , ...? , Julia, !

:

, -, , - . , :

myfun2 <- function(x, ...) 
    UseMethod("my fun")

myfun2.character <- function(x, toLower = FALSE, ...)
    cat("Sent and transformed by myfun:", ifelse(toLower, tolower(x), x), "\n", ...)

myfun2("Test String with CaPiTaLs.", tolower = TRUE)
## Sent and transformed by myfun: Test String with CaPiTaLs. 
##  TRUE
myfun2("Test String with CaPiTaLs.", toLower = TRUE, notDefined = 1)
## Sent and transformed by myfun: test string with capitals. 
##  1
myfun2("Test String with CaPiTaLs.", sep = "\tXX\t")
## Sent and transformed by myfun:   XX  Test String with CaPiTaLs.  XX  
+4
1

. . - :

myfun2.character <- function(x, toLower = FALSE, ...) {
  elli <- names(list(...))
  check <- elli %in% names(formals(cat))
  if (any(!check)) warning(sprintf("%s is not an expected parameter. Did you misstype it?", 
                                   paste(elli[!check], collapse = ", ")))
  cat("Sent and transformed by myfun:", ifelse(toLower, tolower(x), x), "\n", ...)
}


myfun2("Test String with CaPiTaLs.", tolower = TRUE)
#Sent and transformed by myfun: Test String with CaPiTaLs. 
#TRUE
#Warning message:
#  In myfun2.character("Test String with CaPiTaLs.", tolower = TRUE) :
#  tolower is not an expected parameter. Did you misstype it?
myfun2("Test String with CaPiTaLs.", toLower = TRUE, notDefined = 1)
#Sent and transformed by myfun: test string with capitals. 
#1
#Warning message:
#  In myfun2.character("Test String with CaPiTaLs.", toLower = TRUE,  :
#                        notDefined is not an expected parameter. Did you misstype it?
myfun2("Test String with CaPiTaLs.", sep = "\tXX\t")
## Sent and transformed by myfun:   XX  Test String with CaPiTaLs.  XX

, . , , , .

+1

Source: https://habr.com/ru/post/1615765/


All Articles