3

我在 R 中有一个...使用 tidyeval 的简单函数。是否可以将这些更改为字符串?

simple_paste <- function(...){
  my_vars <- enquos(...)
  paste(..., sep = "_x_")
}

simple_paste(hello, world)

作为输出,我想得到"hello_x_world". 我也可以考虑使用glue函数 orstr_c代替paste,尽管我不确定这会更好。

4

1 回答 1

5

将quosure转换为字符,然后paste

simple_paste <- function(...) {
  purrr::map_chr(enquos(...), rlang::as_label) %>% 
          paste(collapse="_x_")
   }
simple_paste(hello, world)
#[1] "hello_x_world"

或者另一种选择是eval评估一个表达式

simple_paste <- function(...)  eval(expr(paste(!!! enquos(...), sep="_x_")))[-1]
simple_paste(hello, world)
#[1] "hello_x_world"

如果我们.csv最后需要

simple_paste <- function(...)  eval(expr(paste0(paste(!!! enquos(...), sep="_x_"), ".csv")))[-1]
simple_paste(hello, world)
#[1] "hello_x_world.csv"
于 2020-03-11T21:19:26.600 回答