代码之家  ›  专栏  ›  技术社区  ›  NelsonGon phoxis

使用deparse(替换)替换多个字符串

  •  1
  • NelsonGon phoxis  · 技术社区  · 7 年前

    deparse(substitute) 看起来很有用。但是,如何将其用于几个字符串。 工作示例:

    print_name<-function(x){
    x<-deparse(substitute(x))
    print(x)
    }
    

    我怎样才能为这样的事情工作?

    print_name<-function(x,...){
      x<-deparse(substitute(x))
      y<-deparse(substitute(...))
      print(x)
      print(y)
    }
    
    print_name(Peter,John,Alice)
    

    除了停在(on?)约翰外,上述方法几乎都能奏效。我怎样才能使它适用于所有名称。那是印刷品彼得、约翰、爱丽丝。 谢谢

    1 回复  |  直到 7 年前
        1
  •  3
  •   MrFlick    7 年前

    你可以这样做

    print_name<-function(x,...){
      x<-deparse(substitute(x))
      y<-sapply(substitute(...()), deparse)
      print(x)
      print(y)
    }
    
    print_name(Peter,John,Alice)
    # [1] "Peter"
    # [1] "John"  "Alice"
    

    如果我们这样做 substitute(...())