在编写配备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个顶部单词)。
这是我的配置:
谢谢,
于连
答案 0 :(得分:0)
作为此seems to be caused by how the svg plots are embedded in the html page,您可以通过从绘图选项中删除SVG图来解决此问题:
options(jupyter.plot_mimetypes = c("text/plain", "image/png" )) # no more svg...