0

I am trying to extract the name of the list I am passing to a function as a string. The string extracted must be stored within the same function (so I can use it during the same call).

Example:

my_function <- function(obj, arg = NULL) {
  # obj is a list object that I pass to my_function()
  obj_string <- ... # obj_string is the name of the list I have just passed to my_function
}

so, if I write my_function(list_name), obj_string must store "list_name".

I tried with deparse(substitute(obj)) or deparse(substitute(Transform)) but they don't work. I have also tried {{obj}} but it won't work either.

GNicoletti
  • 192
  • 2
  • 17
  • `deparse(substitute(obj))` should work – akrun Sep 23 '21 at 18:49
  • If that doesn't work, make sure to provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in your question. – MrFlick Sep 23 '21 at 19:46

3 Answers3

1

We can use deparse/substitute. We don't need to create an object inside

 my_function <- function(obj, arg = NULL) { 
     deparse(substitute(obj))
}

-testing

> my_function(list_name)
[1] "list_name"

If we don't return anything. Just assign to an object and get the value or wrap with () to print into console

 my_function <- function(obj, arg = NULL) { 
    obj_string <- deparse(substitute(obj))
}

-testing

> tmp <- my_function(list_name)
> tmp
[1] "list_name"
> > (my_function(list_name))
[1] "list_name"
akrun
  • 874,273
  • 37
  • 540
  • 662
  • this is weird, when I run deparse(substitute(obj)) I get back an insanely long output which looks more or less the content of the list... I wonder if I am using some package that is replacing the original deparse or substitute functions... – GNicoletti Sep 23 '21 at 18:58
  • @GNicoletti Can you try on a fresh R session wihtout any packages loaded – akrun Sep 23 '21 at 18:59
  • 1
    I will, in the meantime - thank you very much for your answer! I also managed to find a solution myself (look the answer below) – GNicoletti Sep 23 '21 at 18:59
  • I thought your question was about not printing the output into console as you did the `obj_string <-` in the last step – akrun Sep 23 '21 at 19:01
  • 1
    yes you are right, I wrote int the comment that it returns "list_name" but I meant that I successfully managed to store the correct string :) – GNicoletti Sep 23 '21 at 19:03
  • 1
    Or if you want to print on the console just wrap with `(my_function(list_name)) ` – akrun Sep 23 '21 at 19:04
  • deparse(substitute(obj)) works if I assign it to obj_string, but it doesn't if you just call it without assigning it. Thanks for the answer. Solved. – GNicoletti Sep 23 '21 at 19:07
  • 1
    That is why I said to wrap with `()` – akrun Sep 23 '21 at 19:29
1

I finally managed to make it work myself (thanks to the other users who posted so far, I am going to test your answers too!)

as.character(as.list(match.call()[-1]$obj))

returns "list_name"

GNicoletti
  • 192
  • 2
  • 17
0

You need to either return obj_string at the end of the function, or assign (<<-) it to the environment:

my_function <- function(obj, arg = NULL) {
  obj_string <- deparse(substitute(obj))
  obj_string
}

 #OR#

my_function <- function(obj, arg = NULL) {
  obj_string <<- deparse(substitute(obj))
}
GuedesBF
  • 8,409
  • 5
  • 19
  • 37