1

I'm receiving strange error when trying to run an R script using the phangorn package. Whenever I run the script using: Rscript mini_example.R It fails with the error message:

Error in as.vector(data) : 
no method for coercing this S4 class to a vector
Calls: pml ... as.matrix -> as.matrix.default -> array -> as.vector

Which could suggest some Namespace troubles. However when running R CMD BATCH mini_example.R OUT.txt everything runs smooth, no errors.

Code mini_example.R:

library(phangorn)
sessionInfo()
data(Laurasiatherian)
dm <- dist.ml(Laurasiatherian)
tree <- NJ(dm)
fitJC <- pml(tree, Laurasiatherian)  
fitJC <- optim.pml(fitJC)
print('==>>Success <<==') 

I've compared the sessionInfo() for both the Rscript and the R CMD BATCH (see below) and the only difference is that methods is not attached when using Rscript but loaded via Namespace. I'm a bit confused about this behavior, so it would be great if someone with more knowledge might have an idea what might cause the Rscript trouble...

For R CMD BATCH

R version 3.2.1 (2015-06-18)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.9.5 (Mavericks)

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] phangorn_1.99.14 ape_3.3         

loaded via a namespace (and not attached):
[1] Matrix_1.2-2    nnls_1.4        parallel_3.2.1  igraph_0.7.1   
[5] nlme_3.1-120    grid_3.2.1      lattice_0.20-31 quadprog_1.5-5 

For Rscript

R version 3.2.1 (2015-06-18)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.9.5 (Mavericks)

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  base     

other attached packages:
[1] phangorn_1.99.14 ape_3.3         

loaded via a namespace (and not attached):
[1] Matrix_1.2-2    nnls_1.4        parallel_3.2.1  igraph_0.7.1   
[5] nlme_3.1-120    grid_3.2.1      methods_3.2.1   lattice_0.20-31
[9] quadprog_1.5-5 
Fabian_G
  • 431
  • 4
  • 16

1 Answers1

2

This works with Rscript if you import library(methods) in addition to phangorn. This changes the sessionInfo() for methods from "loaded via a namespace (and not attached)" to "attached base packages", which was the only difference between your sessionInfo() for Rscript vs R CMD BATCH.

I don't know why the two would differ. However, this answer remarks that littler (an alternative to Rscript) "load the methods package", which would suggest that the authors of littler have experienced your issue too.

Community
  • 1
  • 1
Jon Olav Vik
  • 1,421
  • 2
  • 12
  • 20
  • Your solution works. Citing from `?Rscript`: Rscript omits methods as it takes about 60% of the startup time... So that's the reason, wasn't aware of that. – Fabian_G Sep 07 '15 at 19:01