我在R中有一个简单的函数,它使用tidyeval的...。是否可以将这些转换为字符串?
simple_paste <- function(...){
my_vars <- enquos(...)
paste(..., sep = "_x_")
}simple_paste(hello, world)
作为输出,我想得到"hello_x_world"。我也可以考虑使用glue函数或str_c来代替paste,尽管我不确定这样做会更好。
发布于 2020-03-11 21:19:26
将质量转换为字符,然后转换为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"https://stackoverflow.com/questions/60644445
复制相似问题