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.
There are a few small issues.
ifelse
is a vectorized function, but you just need a simpleif
. In fact, you don't really need anelse
-- 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
andb
instead of"a"
and"b"
. Instead of theds$x
syntax, you should use theds[[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 wantdeparse(substitute())
instead ofdeparse(quote())