0
votes

{R version 3.1.2}

I am trying to make a function that can take the name of any txt file in the working directory and automatically read it, remove stop words, and create a word cloud. Here is the code so far:

word_cloud <- function(x){
    fileName <- paste("./", x , sep="")
    txt = readLines(fileName, warn=FALSE)
    myCorpus = Corpus(VectorSource(txt))

    myCorpus = tm_map(myCorpus, tolower)
    myCorpus = tm_map(myCorpus, removePunctuation)
    myCorpus = tm_map(myCorpus, removeNumbers)
    myCorpus = tm_map(myCorpus, removeWords, stopwords("english"))

    myDTM = TermDocumentMatrix(myCorpus, control = 
                                            list(minWordLength = 1))

    m = as.matrix(myDTM)

    v = sort(rowSums(m), decreasing = TRUE)

    set.seed(4363)
    wordcloud(names(v), v, min.freq = 50)
}          

When it runs down to the line where it assigns myDTM, it throws the error,

"Error in UseMethod("meta", x) : no applicable method for 'meta' applied to an object of class "character""

and the warning:

"In addition: Warning message: In mclapply(unname(content(x)), termFreq, control) : all scheduled cores encountered errors in user code"

What's going on here?

2
I can't reproduce your error without some data. Can you give the results of: txt = readLines(fileName, warn=FALSE); dput(head(txt)) - Chris
c("This article was downloaded by: [University of California, San Diego]", "On: 01 April 2014, At: 13:41", "Publisher: Routledge", "Informa Ltd Registered in England and Wales Registered Number: 1072954 Registered office: Mortimer House, 37-41 Mortimer Street, London W1T 3JH, UK", "Ethics, Policy & Environment", "Publication details, including instructions for authors and subscription information: tandfonline.com/loi/cepe21" ) - Grant Oliveira
Alternatively you can download it for yourself here: drive.google.com/file/d/0B0NcCsbmnPd1em1FR1d3V1NubDA/… - Grant Oliveira

2 Answers

1
votes

Add one more line of code -

myCorpus <- tm_map(myCorpus, PlainTextDocument)
0
votes

readLines is likely not creating a length-1 vector like you are assuming it to. When I read the file in It creates a 198 length vector, which VCorpus is going to think is 198 documents. Try adding this after readLines:

paste(txt, collapse = " ") -> txt

When I run your code with that addition it works