Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return name of data frame from function

Tags:

r

I have the following function where there is only one parameter, df. df is dataframe:

test_function <- function(df) {
    df_name <- df #get name of dataframe (does not work)
    df_name
  }

test_function(mtcars)

How do I return name of the dataset from this function? For test_function(mtcars) I need to assign string mtcars to df_name.

like image 1000
Tomas Greif Avatar asked Jan 24 '26 04:01

Tomas Greif


1 Answers

You can use the combo substitute + deparse

test_function <- function(df)
    deparse(substitute(df))

test_function(mtcars)
##[1] "mtcars"
like image 113
dickoa Avatar answered Jan 26 '26 16:01

dickoa