以下代码在编写配备 R 内核的 ipython 笔记本时可以正常工作。不幸的是,第二个条形图导出到 html 失败(都带有 jupyter 的嵌入式选项和手动使用 nbconvert)。
library(NLP)
library(tm)
# here I used the EBook of Ulysses, by James Joyce, but any text file can fit
# the text is available here: https://www.gutenberg.org/cache/epub/4300/pg4300.txt
book <- readLines("pg4300.txt", encoding="UTF-8")
corpus <- Corpus(VectorSource(book))
corpus <- tm_map(corpus, content_transformer(tolower))
corpus <- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, removePunctuation)
dtm <- TermDocumentMatrix(corpus)
m <- as.matrix(dtm)
freq <- rowSums(m)
freq.sorted <- sort(freq, decreasing=TRUE)
# first barplot with stop words (ok for both notebook and export)
barplot(freq.sorted[1:50], xlab="Word", ylab="Frequency", las=2)
corpus.sw <- tm_map(corpus, removeWords, stopwords('english'))
dtm.sw <- TermDocumentMatrix(corpus.sw)
m.sw <- as.matrix(dtm.sw)
freq.sw <- rowSums(m.sw)
freq.sw.sorted <- sort(freq.sw, decreasing=TRUE)
# second barplot without stop words (ok on ipython notebook but fail when exporting)
barplot(freq.sw.sorted[1:50], xlab="Word", ylab="Frequency", las=2)
真正奇怪的是,第一个条形图很好地导出,但不是第二个,而过程完全相同(显示前 50 个单词)。
这是我的配置:
- macosx 10.11.2 埃尔卡潘
- jupyter 4.0.6
- ipython 4.0.1
- R 版本 3.2.2
谢谢,
朱利安