使用dplyr进行管道传输时获取lhs对象名称 [英] get lhs object name when piping with dplyr

查看:104
本文介绍了使用dplyr进行管道传输时获取lhs对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个可以使用从dplyr导出的管道运算符的函数.我不使用magrittr.

I'd like to have a function that can use pipe operator as exported from dplyr. I am not using magrittr.

df %>% my_function

如何获得df名称?如果我尝试

How can I get df name? If I try

my_function <- function(tbl){print(deparse(substitute(tbl)))}

它返回

[1] "."

我想拥有 [1]"df"

while I'd like to have [1] "df"

有什么建议吗?

预先感谢您,
尼古拉

Thank you in advance,
Nicola

推荐答案

JBGruber

The SO answer that JBGruber links to in the comments mostly solves the problem. It works by moving upwards through execution environments until a certain variable is found, then returns the lhs from that environment. The only thing missing is the requirement that the function outputs both the name of the original data frame and the manipulated data – I gleaned the latter requirement from one of the OP's comments. For that we just need to output a list containing these things, which we can do by modifying MrFlick's answer:

get_orig_name <- function(df){
    i <- 1
    while(!("chain_parts" %in% ls(envir=parent.frame(i))) && i < sys.nframe()) {
        i <- i+1
    }
    list(name = deparse(parent.frame(i)$lhs), output = df)
}

现在,我们可以在任何管道的末尾运行get_orig_name,以获取列表中的经处理数据和原始数据帧的名称.我们使用$:

Now we can run get_orig_name to the end of any pipeline to the get the manipulated data and the original data frame's name in a list. We access both using $:

mtcars %>% summarize_all(mean) %>% get_orig_name

#### OUTPUT ####

$name
[1] "mtcars"

$output
       mpg    cyl     disp       hp     drat      wt     qsec     vs      am   gear   carb
1 20.09062 6.1875 230.7219 146.6875 3.596563 3.21725 17.84875 0.4375 0.40625 3.6875 2.8125

我还应该提到,尽管我认为该策略的细节很有趣,但我也认为它不必要地复杂.听起来OP的目标是处理数据,然后将其写入与原始未经处理的数据帧同名的文件中,这可以使用更直接的方法轻松完成.例如,如果我们要处理多个数据帧,则可以执行以下操作:

I should also mention that, although I think the details of this strategy are interesting, I also think it is needlessly complicated. It sounds like the OP's goal is to manipulate the data and then write it to a file with the same name as the original, unmanipulated, data frame, which can easily be done using more straightforward methods. For example, if we are dealing with multiple data frames we can just do something like the following:

df_list <- list(mtcars = mtcars, iris = iris)

for(name in names(df_list)){
    df_list[[name]] %>% 
        group_by_if(is.factor) %>%
        summarise_all(mean) %>% 
        write.csv(paste0(name, ".csv"))
}

这篇关于使用dplyr进行管道传输时获取lhs对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆