5

I have a question about Reference Classes. My question is in the context of an R package I am developing rCharts. It uses reference classes to create interactive plots from R.

Creating a plot involves a series of calls. Here is an example, where a scatterplot is created at first and then a line plot gets added.

p1 <- rPlot(mpg ~ cyl, data = mtcars, type = 'point')
p1$layer(copy_layer = T, type = 'line')

Now, since a Reference Class is like a closure, I was wondering if it was possible to log the calls made. The idea is that if I can log the sequence of calls made, then I can automagically insert the source code used to create a visualization, along with the html.

I was trying to see if I could make use of sys.function or match.call, but am not getting anywhere. If someone can point me to how I can approach this, it would be much appreciated.

Ramnath
  • 54,439
  • 16
  • 125
  • 152
  • 2
    The solution should be fairly: in each method you'll need something like `calls <<- c(calls, list(match.call()))` – hadley Apr 19 '13 at 12:31
  • Thanks, that worked great! Maybe I should post this as a different question, but what is the recommended way to rerun a list of calls. – Ramnath Apr 19 '13 at 13:46
  • 2
    Just `eval()` them - but if you want to do that, you'll also need to capture the environment in which they were executed with `parent.frame()` – hadley Apr 19 '13 at 17:58
  • Thanks! I realized that I don't need to eval them, since the reference e class already contains the output of all the calls. The logging is only to record the sequence of calls. If you post as an answer, I can accept it. Thanks again. – Ramnath Apr 19 '13 at 19:08

1 Answers1

2

As @hadley stated:

calls <<- c(calls, list(match.call()))

Glad that looks to have worked. Let's get this closed. :)

Thell
  • 5,883
  • 31
  • 55