Looks like R finds a formula where it expects a data frame. R considers "function" =
as.data.frame.default(x[], optional = TRUE)is not a dataframe. thats why you got the error mssage.
Verify str("function") and check it.
There is online manual here, you can download it
http://cran.r-project.org/doc/manuals/R-intro.pdf