I have a question that I know many times answered in this page, I tried all of them but unfortunately any of them did not work for me. I am pretty new in R and I read a lot but I could not find the answer. I really appreciate if anybody can help me.
I put my code here the error is " Error in UseMethod("meta", x) : no applicable method for 'meta' applied to an object of class "try-error" "
here is my code,
#init
libs <- c("tm" , "plyr" , "class", "wordcloud", "SnowballC" )
lapply(libs, require, character.only = TRUE)
#set options
options(stringsAsFactors = FALSE)
#set parameters
candidates <- c("obama","romney")
pathname <- "/home/sahar/R/sample-text/speeches"
#clean text
cleancorpus <- function(corpus)
{
corpus.tmp <- tm_map(corpus, removeNumbers, lazy = TRUE)
print( corpus.tmp)
corpus.tmp <- tm_map(corpus, removepunctuation, lazy = TRUE)
print( corpus.tmp)
corpus.tmp <- tm_map(corpus.tmp, stripwhitespace, lazy = TRUE )
print( corpus.tmp)
corpus.tmp <- tm_map(corpus.tmp, content_transformer(tolower), lazy = TRUE)
print( corpus.tmp)
#corpus.tmp <- tm_map(corpus.tmp, tolower)
# corpus.tmp <- tm_map(corpus.tmp, PlainTextDocument)
corpus.tmp <- tm_map(corpus.tmp, removewords, stopwords("english"), lazy = TRUE)
print( corpus.tmp)
corpus.tmp <- tm_map(corpus.tmp, stemDocument, lazy = TRUE)
print( corpus.tmp)
# corpus.tmp<- tm_map(corpus.tmp, content_transformer(tolower(x) iconv(x, to='UTF-8-MAC', sub='byte')), mc.cores=1)
# wordcloud(corpus.tmp)
return(corpus.tmp)
}
#build TDM
generateTDM <- function(cand, path)
{
s.dir <- sprintf("%s/%s" , path, cand)
# s.cor <- Corpus(DirSource(directory = s.dir, encoding = "ANSI"))
print(s.dir)
s.cor <- VCorpus(DirSource(directory = s.dir), readerControl = list(reader = readPlain))
print(s.cor)
# s.cor <- tm_map(s.cor,
# content_transformer(function(x) iconv(x, to='UTF-8-MAC', sub='byte')),
# mc.cores=1
# )
s.cor.cl <- cleancorpus(s.cor)
print(s.cor.cl)
s.tdm <- TermDocumentMatrix(s.cor.cl)
print(s.tdm)
s.tdm <- removeSparseTerms(s.tdm, 0.7)
print(s.tdm)
result <- list(name = cand, tdm = s.tdm)
}
tdm <- lapply(candidates, generateTDM, path = pathname)
traceback()
. The dispatch can't find a method forclass(try-error)
, which is an output from atry
statement when the expression fails to evaluate. Your code doesn't have atry
in it, so it has to be buried in one of the functions you called.corpus.tmp <- tm_map(corpus.tmp, removepunctuation, lazy = TRUE)
, withcorpus.tmp
instead ofcorpus
.