11

I have simple R script which trying to Define class. example.R

 Tuple <- setClass("Tuple",
            slots = c(
              id="character",
              comp="character",
              stream="character",
              task="character",
              output="vector",
              anchors="vector"
                )
            );

when i run Rscript example.R . i am getting an error saying below

Error in eval(expr, envir, enclos) : could not find function "setClass" Calls: source -> withVisible -> eval -> eval

can any one help?

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
user2862709
  • 153
  • 1
  • 6

1 Answers1

17

Rscript, in all its wisdom, does not load the standard methods package. So precede your script with

library(methods)

and all should be good.

If you're on Linux, you could use our littler package. It loads methods for you too, and starts a little faster than Rscript too.

Edit It is now some five years later and this has been added to Rscript in R release 3.5.0 or later.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 1
    Or call with the namespace: `methods::setClass(...` – James Dec 17 '13 at 13:22
  • As an additional note - if you have this problem when running checks on a package (if you're using devtools to run checks, for example) - you can add `import(methods)` to your NAMESPACE. – sebastian-c Jun 02 '16 at 15:46