0

I have a function that takes a call xm, where xm is a learnt machine learning model. Is there a way tht within the function I can print the name of xm rather than the summary of the model which is what happens when you print(xm)

For example, my function generates graphs that I am saving within the function

modsummary <- function(xm){

    mypath <- file.path("C:","Users","Documents",paste("rf_fit_hmeas_random", ".png", sep = ""))

    png(file = mypath)

    print(plot(xm))
    dev.off()
}

modsummary(rf_fit)

What I am trying to do is set this up in way so that it will paste xm (in this case rf_fit) so that it automatically detecs the function called and replaces xm_hmeas_random each time a different model is called.

Thank you

  • 1
    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) – Thomas K Oct 25 '18 at 08:12

1 Answers1

0

Yes, you can use deparse(substitute(xm)) to get this.

So it would be..

modsummary <- function(xm){

    mypath <- file.path("C:/","Users/","Documents/",paste0(deparse(substitute(xm)), "_hmeas_random.png"))

png(file = mypath)

print(plot(xm))
dev.off()
}

modsummary(rf_fit)

I've added in the slashes (/) between your directory names and switched to using paste0() which negates the need to specify a separator.

slackline
  • 2,295
  • 4
  • 28
  • 43