title: In R, getting the following error: "attempt to replicate an object of type 'closure'"
body: <p>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:</p>
  <pre><code>mydata &lt;- data.frame(a = rnorm(100), b = rnorm(100,0,.2))
  plot(mydata$a, mydata$b) # works just fine
  
  scatter_plot &lt;- function(ds) { # function I'm trying to create
      ifelse(exists(deparse(quote(ds))),
          function(x,y) plot(ds$x, ds$y),
              sprintf(&quot;The dataset %s does not exist.&quot;, ds))
      }
  
  scatter_plot(mydata)(a, b) # not working
  </code></pre>
  <p>Here's the error I'm getting:</p>
  <blockquote>
  <p>Error in rep(yes, length.out = length(ans)) :
  attempt to replicate an object of type 'closure'</p>
  </blockquote>
  <p>I tried several other versions, but they all give me the same error.  What am I doing wrong?</p>
  <p>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.</p>