1

How can I pass a character vector using NSE:

fun <- function(x){
  x_  <-deparse(substitute(x))
  print(x_)
}

fun_ <- function(x){
 do_something(x)
}

For example,

fun(x= a, b, c)

should print interpret the argument to x as a vector c(a, b, c) and pass a character vector to fun_(x):

fun_(x=c("a", "b", "c"))    

There are additional parameters as well.

user151410
  • 776
  • 9
  • 22

1 Answers1

3

You can't use commas within a parameter; commas separate parameters. Calling fun(x=a,b,c) would call fun() with three parameters, the first one named and the second two unnamed. If you just want to ignore the name x=, you can turn unquoted names to strings with

fun <- function(...){
  x <- sapply(substitute(...()), deparse)
  fun_(x)
}

fun_ <- function(x) {
    print(x)
}

fun(a,b,c)
# [1] "a" "b" "c" 

fun_(c("a","b","c"))
# [1] "a" "b" "c"
Alex A.
  • 5,466
  • 4
  • 26
  • 56
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • The `...()` command is seriously mind-bending. If you want help to digest it check out this question: [Confused by ...()?](http://stackoverflow.com/questions/12523548/confused-by) – Backlin May 07 '15 at 15:09