get lhs object name when piping with dplyr

后端 未结 5 2116
遥遥无期
遥遥无期 2021-02-13 15:43

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

df %>% my_function

How can I get df

5条回答
  •  甜味超标
    2021-02-13 16:06

    Inspired by the link mentioned by gersht

    You can go back 5 generations to get the name

    df %>% {parent.frame(5)$lhs}
    

    example as below:

    library(dplyr)
    
    a <- 1
    
    df1 <- data.frame(a = 1:10)
    
    df2 <- data.frame(a = 1:10)
    
    a %>% {parent.frame(5)$lhs}
    
    df1 %>% {parent.frame(5)$lhs}
    
    df2 %>% {parent.frame(5)$lhs}
    

提交回复
热议问题