2

I am trying to knit a chunk embedded in a latex file with extension Rnw.

library(ggplot2)
library(flexclust)
library(arules)

data(Groceries)

Gr <- Groceries[, itemFrequency(Groceries) > 0.02]
grc <- as(Gr, "matrix")

I get this error:

#Error in as(Gr, "matrix") could not find function "as"

The same commands work perfectly well in R

Any ideas?

3
  • 2
    doesn't as() come from the methods-package?
    – Wimpel
    Commented Jun 13, 2018 at 10:24
  • Possible duplicate of stackoverflow.com/questions/7027288/…
    – zx8754
    Commented Jun 13, 2018 at 10:29
  • Interestingly enough, adding the library(methods) inside the knitr body solved the problem. Kudos to Wimpel and thank you!
    – Dan
    Commented Jun 13, 2018 at 11:48

1 Answer 1

0

When in doubt, try to upgrade your software packages. If you are using a recent version of R (>= 3.5.0), you should not need to library(methods), because it will be loaded (attached) by default. This problem (Rscript does not load methods) has existed in base R for many years until this year and surprised/confused many users. If you have to use an old version of R, you will need library(methods) explicitly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.