问题
I'm using the "topicmodels" package in R. Everything works fine interactively, but if I run the exact same commands using Rscript, I get errors.
The first error (which I solved) is that R didn't know what the is() function was. I solved this by importing the "methods" package. Apparently, Rscript doesn't import this automatically, even though interactive R does, and this caused a problem with is(). Problem solved.
However, I am now stuck at a different error, which I can't figure out. I am using the LDA() function in the "topicmodels" package, using data (in DTM format) and k=10. I call it like this:
library(plyr)
library(lda)
library(topicmodels)
x = as.data.frame(sapply(1:100, function(x) sample(1:100,100,replace=T)))
u = llply(colnames(x), function(a) rbind(0:(length(x[,a])-1),x[,a]))
v = rownames(x)
y = ldaformat2dtm(u, v)
a = LDA(x, 10)
And it gives me the following error:
> Error in as(control, "LDA_VEMcontrol") :
> no method or default for coercing "NULL" to "LDA_VEMcontrol"
> Calls: LDA -> method -> as
> Execution halted
The main thing is this works interactively, but not using Rscript. I know the data is correctly formatted and if I print the data, it looks good. Is there something else I'm missing? Are there other modules that Rscript doesn't load, but R interactive does load? Thanks!
回答1:
I just ran the example via Rscript and via source() in an interactive session, both worked. The only output from Rscript was:
% Rscript /tmp/sc.r
Loading required package: methods
So it seems to have figured out the methods thing on its own.
I have R 3.0.1, maybe you have an older version of R or one of the packages? They may have updated their prereqs list to include methods.
来源:https://stackoverflow.com/questions/19780515/can-use-package-interactively-but-rscript-gives-errors