Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setClass not found when running R script from command line

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?

like image 772
user2862709 Avatar asked Dec 17 '13 13:12

user2862709


People also ask

How do I run an Rscript from the command line?

Running R from the Command Line To open up the command prompt, just press the windows key and search for cmd. When R is installed, it comes with a utility called Rscript. This allows you to run R commands from the command line.

How do I run a .R file?

To run an R command, put the cursor on the line of the command and then click the Run button at the top of the file window. Or just press CTRL-Enter.

How do I run a Rscript in R studio?

You can open an R script in RStudio by going to File > New File > R script in the menu bar. RStudio will then open a fresh script above your console pane, as shown in Figure 1-7.


1 Answers

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.

like image 157
Dirk Eddelbuettel Avatar answered Oct 14 '22 17:10

Dirk Eddelbuettel