object of type 'closure' is not subsettable - contradiction?

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2012-12-01T22:54:06Z Indexed on 2012/12/01 23:03 UTC
Read the original article Hit count: 731

Filed under:
|

I'm writing a function to produce time series plots of stock prices. However, I'm getting the following error

"Error in df[, 7] : object of type 'closure' is not subsettable"

Here's an example of the function:

plot.prices <- function(df) {
  require(ggplot2)
  g <- ggplot(df, aes(x= as.Date(Date, format= "%Y-%m-%d"),
              y= df[, 7])) + geom_point(size=1)

  # ... code not shown...
  g
}

And example data:

spy <- read.csv(file= 'http://ichart.finance.yahoo.com/table.csv?s=SPY&d=11&e=1&f=2012&g=d&a=0&b=29&c=1993&ignore=.csv', header= T)

plot.prices(spy) # produces error
ggplot(spy, aes(x= as.Date(Date, format= "%Y-%m-%d"),
                  y= spy[, 7])) + geom_point(size=1) 
## does not produce error

As you can see, the code is identical. I get an error if the call to ggplot() is INSIDE the function but not if the call to ggplot() is OUTSIDE the function.

Anyone have any idea why the seeming contradiction?

© Stack Overflow or respective owner

Related posts about r

    Related posts about ggplot2