1

I have a number of objects I would like to plot in R and save them to my directoy. To assign the name of each graph I need to be able to obtain the name (as String?) of the object to plot.

An example is:

setwd("C:/.../mydirectory/")
myname <- myobjectname(myobject) **I want to obtain the string of "myobject"**
png(paste(myname, ".png", sep=""))
myGraphingFunction(myobject)
dev.off()

I have tried the:

myobjectname <- function(v1){
  deparse(substitute(v1))
}

Which does what I want to do and returns the string I want. Nevertheless, when using it in a loop or in the above graph case it is not working.

I have a function with the argument "month" which will be substituted by "january" or "february", etc. E.G. When using the function:

myobjectname <- function(month) 

Within a loop, altough the variable month is now January (or whatever), the string I get is always "month".

Summarizing:

> deparse(substitute(january))
[1] "january"

> month <- january
> deparse(substitute(month))
[1] "month"

Any hint on how for the second case to give back "january" ?

Thanks

alvaropr
  • 699
  • 9
  • 20
  • 1
    Just print the variable `month`? Also, the function `function(month)` doesn't do anything.. – talat May 31 '17 at 10:14
  • Possible duplicate of [In R, how to get an object's name after it is sent to a function?](https://stackoverflow.com/questions/10520772/in-r-how-to-get-an-objects-name-after-it-is-sent-to-a-function) – Fábio Oct 28 '17 at 22:44

2 Answers2

6

I hope I've understood correctly:

myfun <- function(month) {
  deparse(match.call()$month)
}

january <- 1

myfun(january)
#[1] "january"

Regarding your "Summarizing" example: It's not possible.

I suspect your question is lacking a representative example. You've minimized too much.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Sorry. It is the first time I use StackOverFlow and that I am learning R so my bad I have not been clear enough. Ill try now – alvaropr May 31 '17 at 12:26
  • I will try to simplify it. I have 12 dataframe (one per month with the month name). I want to plot some info of each one and I wrote a function _mygraphfun_. I want the name of the month in the title: `mygraphfun <- function(Mymonth,...){ ggplot(Mymonth, aes(x = Date)) + geom_line(aes(y = amount), color = "red") + ggtitle(Mymonth) }` My doubt was regardingg how to pass the string name of _Mymonth_ to _ggtitle_. I try to explained in another way and I just made u more confused maybe. Sorry. – alvaropr May 31 '17 at 12:28
  • Are your data.frames together in a list (as they should be)? Then you could simply iterate over the list names. – Roland May 31 '17 at 14:29
  • Hehe, they are not. This seems like the easiest things. Anyways I miss a simple function that you ask R the name of a dataframe and it gives it back as string (being able to store in a variable). I guess there must be a reason but as a newby I still dont get it :P Thanks – alvaropr Jun 01 '17 at 10:29
  • The line `deparse(match.call()$arg)` breaks when processing in parallel! I get a `unable to create dataset` error. It took me forever to figure out what was going wrong, but I finally found it. Any insight as to why this is happening? – philiporlando Sep 18 '18 at 01:42
2

In month <- "january", you have "filled" the variable with the name 'month' with the value 'january'. If you deparse(substitute()) that variable, you get the name of that variable, which is 'month'. If you want the value as a string, just print the variable, as @docendodiscimus suggested in their comment, which will return the value, which already is a string!

> month <- "january"
> class(month)
[1] "character"
> month
[1] "january"

Note:

Your code, month <- january (without quotations marks), does not make sense, unless 'january' is a variable:

> month <- january
Error: object 'january' not found

but:

> january <- "whatever"
> month <- january
> month
[1] "whatever"