How to pass some (but not all) further arguments with “…”

后端 未结 2 1241
轮回少年
轮回少年 2020-12-21 02:35

One of my in-progress functions calls grep() with value = TRUE hard-coded. I\'d like to pass all of the further arguments except <

相关标签:
2条回答
  • 2020-12-21 03:24

    You can combine Curry with do.call:

    require(functional)
    f <- function(pattern, x, ...)
    {
      dots <- list(...)
      dots <- dots[!grepl('value', names(dots))]
      do.call(Curry(grep, pattern=pattern, x=x, value=TRUE), dots)
    }
    

    Curry provides the known arguments, and dots supplies everything other than "value" in ....

    0 讨论(0)
  • 2020-12-21 03:30

    One way is to just make value a named parameter but ignore the input - that way it will never be a part of the dots to begin with.

    f <- function(pattern, x, value = "hahaThisGetsIgnored", ...){
       grep(pattern, x, value = TRUE, ...)
    }
    

    You can also use the idea in the answer @MatthewLundberg gave but by doing the argument manipulation without Curry so you don't need the functional package

    f <- function(pattern, x, ...){
      dots <- list(...)
      # Remove value from the dots list if it is there
      dots$value <- NULL
      args <- c(list(pattern = pattern, x = x, value = TRUE), dots)
      do.call(grep, args)
    }
    
    0 讨论(0)
提交回复
热议问题