2

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!

Ken Williams
  • 22,756
  • 10
  • 85
  • 147
a b
  • 171
  • 1
  • 1
  • 10
  • Since you haven't provided `x` to us perhaps you were remiss in providing it to R as well. – Tyler Rinker Nov 05 '13 at 01:40
  • can i attach a 500 mb file? anyway, my question is about Rscript vs. R interactive. is there a way to run Rscript EXACTLY like R interactive? – a b Nov 05 '13 at 01:42
  • No use a subset using `dput(head(x, 15))` and make sure this gives the error as well. – Tyler Rinker Nov 05 '13 at 01:43
  • Ok, something like this should work: 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); tm = LDA(x, 10); – a b Nov 05 '13 at 01:53
  • 1
    Ok so made an R script using your exact code and then named it `test.R` and used `source("test.R")` and it ran. and `tm` is `A LDA_VEM topic model with 10 topics.` so are you using source? I didn't have to import methods. What are the error messages you get. Please post them in your original question with code tags. – Tyler Rinker Nov 05 '13 at 02:02

1 Answers1

2

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.

Ken Williams
  • 22,756
  • 10
  • 85
  • 147