Does it work with R -f script? If so, then it's because Rscript does not attaching methods package by default, but R does. Try loading methods at the top of your script.
My $.02 Henrik On Apr 8, 2015 07:41, "Milt Epstein" <mepst...@illinois.edu> wrote: > Greetings. I am new to R, but have quite a bit of experience > programming with other languages (e.g., Perl, Java, Python, shell > scripting). I'm now working on a project where I need to use R. A > colleague wrote a number of small scripts that work fine in Rstudio, > but a couple of them don't work when run using Rscript (which we're > planning on doing). The behavior is basically the same on a few > different machines (two of them are Linux, one I think is a Mac). To > run the scripts using Rscript, we put one of the following lines at > the top of the script: > > #!/usr/bin/Rscript > #!/usr/bin/env Rscript > > or called the script using Rscript: > > Rscript scriptname > > In all cases, the behavior is the same. > > Here's one of the scripts: > > #!/usr/bin/env Rscript > > library(apcluster) > options(stringsAsFactors = FALSE) > > args <- commandArgs(TRUE) > num <- args[1] > > numClusters <- num > mydata <- read.csv("input_data.csv") > xData <- mydata[, 1] > yData <- mydata[, 2] > > fit <- apclusterK(negDistMat(r=2), mydata, K = numClusters) > #Leave the rest commented for now > #output <- data.frame(xData, yData, c(as.data.frame(fit[1]), > as.data.frame(fit[2]), as.data.frame(fit[3]))) > #write.csv <- write.table(output, file = "output_AP.csv", sep = ",", > row.names = FALSE, col.names = FALSE) > > Here's a call to the script: > > $ ./affinity_propagation.R 3 > > Attaching > > The following object is masked package:: > > heatmap > > Trying p = -15.41969 > Number of clusters: 17 > Error in tmpk - K : non-numeric argument to binary operator > Calls: apclusterK ... apclusterK -> .local -> apclusterK -> apclusterK -> > .local > Execution halted > > The line with the expression "tmpk - K" is from the code for the > apclusterK() function (from the apcluster library). The > definition/value of tmpk is set using a call to the function length(). > I mention this because the failure we're getting with the other script > also seems to involve an expression with call to length(). Here's the > error from calling that script: > > $ ./spectral_clustering.R 3 > Loading required package: methods > Error in length(tmpsig) * nc : non-numeric argument to binary operator > Calls: specc -> specc -> .local -> matrix > Execution halted > > The script is calling the specc() function in the kernlab library. > > Any ideas what's going on here, why it's not working and what we can > do to get it to work? Is there something that needs to be set or run > in .Renviron or .Rprofile, say? > > Thanks. > > Milt Epstein > Programmer in Computational Genomics > Institute for Genomic Biology (IGB) > University of Illinois at Urbana-Champaign (UIUC) > mepst...@illinois.edu > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.