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

后端 未结 2 1256
轮回少年
轮回少年 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: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)
    }
    

提交回复
热议问题