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?

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
Dan
  • 21
  • 2
  • 2
    doesn't as() come from the methods-package? – Wimpel Jun 13 '18 at 10:24
  • Possible duplicate of https://stackoverflow.com/questions/7027288/error-could-not-find-function-in-r – zx8754 Jun 13 '18 at 10:29
  • Interestingly enough, adding the library(methods) inside the knitr body solved the problem. Kudos to Wimpel and thank you! – Dan Jun 13 '18 at 11:48

1 Answers1

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.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419