18
votes

I am trying to write an R function that takes a data set and outputs the plot() function with the data set read in its environment. This means you don't have to use attach() anymore, which is good practice. Here's my example:

mydata <- data.frame(a = rnorm(100), b = rnorm(100,0,.2))
plot(mydata$a, mydata$b) # works just fine

scatter_plot <- function(ds) { # function I'm trying to create
    ifelse(exists(deparse(quote(ds))),
        function(x,y) plot(ds$x, ds$y),
            sprintf("The dataset %s does not exist.", ds))
    }

scatter_plot(mydata)(a, b) # not working

Here's the error I'm getting:

Error in rep(yes, length.out = length(ans)) : attempt to replicate an object of type 'closure'

I tried several other versions, but they all give me the same error. What am I doing wrong?

EDIT: I realize the code is not too practical. My goal is to understand functional programming better. I wrote a similar macro in SAS, and I was just trying to write its counterpart in R, but I'm failing. I just picked this as an example. I think it's a pretty simple example and yet it's not working.

1
What your code is trying to do is too unconventional. Can you say what you are tyring to achieve?asb

1 Answers

18
votes

There are a few small issues. ifelse is a vectorized function, but you just need a simple if. In fact, you don't really need an else -- you could just throw an error immediately if the data set does not exist. Note that your error message is not using the name of the object, so it will create its own error.

You are passing a and b instead of "a" and "b". Instead of the ds$x syntax, you should use the ds[[x]] syntax when you are programming (fortunes::fortune(312)). If that's the way you want to call the function, then you'll have to deparse those arguments as well. Finally, I think you want deparse(substitute()) instead of deparse(quote())

scatter_plot <- function(ds) {
  ds.name <- deparse(substitute(ds))
  if (!exists(ds.name))
    stop(sprintf("The dataset %s does not exist.", ds.name))
  function(x, y) {
    x <- deparse(substitute(x))
    y <- deparse(substitute(y))
    plot(ds[[x]], ds[[y]])
  }
}
scatter_plot(mydata)(a, b)