将符号向量作为函数参数传递并转换为字符向量

Pass vector of symbols as function argument and convert to character vector

我有一个函数,我想将一个符号向量作为参数传递,然后在内部我想将该向量转换为字符向量。

最小示例:

fun <- function(symbols = c(a, b, c)) {
 # code to convert to character vector 
}

fun()

输出:

[1] "a" "b" "c"

这是 rlang::quo_name 的方法:

library(rlang)
fun <- function(symbols = c(a, b, c)) {
  symbols <- enquo(symbols)
  string <- quo_name(symbols)
  unlist(strsplit(gsub("(c\(|\)|\s)","",string),","))
}

fun(c(apple, orange, pear))
#[1] "apple"  "orange" "pear"

我怀疑你实际上是在尝试解决另一个问题,所以 post that 作为另一个问题可能是有意义的。

基础 R 解决方案:

fun <- function(symbols = c(a, b, c)) {
  # code to convert to character vector
  return(unlist(strsplit(
    gsub("c\(|\)|\(|\s+", "",
         deparse(substitute(symbols))), ","
  )))
}

fun()