你好R专家,
我有一个词云,但我想把它变成一个html文件,所以其他人可以在他们的网站上使用这个wordcloud。那么..我试过R2HTML,但我需要一些指导来处理png文件。 这是我的代码:
library(tm)
library(RTextTools)
library(reshape)
library(plyr)
library(ggplot2)
library(stringr)
library(wordcloud)
library(RColorBrewer)
library(R2HTML)
c <- "HTML frame creates framed output, with commands in the left frame, linked to output in the right frame. By default, a CSS file named R2HTML.css controlling page look and feel is output to the same directory. Optionally, you can include a CSSFile= option to use your own formatting file"
corpus<- Corpus(VectorSource(c))
corpus<- tm_map(corpus, tolower)
corpus<- tm_map(corpus, removePunctuation)
corpus<- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, removeWords, myStopwords)
dictCorpus<- corpus
corpus<- tm_map(corpus, stemDocument)
dtm<- DocumentTermMatrix(corpus)
dtm
dtm.df <- as.data.frame(inspect(dtm))
library(reshape)
dtm2.df <- t(dtm.df)
topx <- as.matrix(dtm2.df)
forwc <- sort(rowSums(topx),decreasing=TRUE)
forwc2 <- data.frame(word = names(forwc),freq= forwc)
pal1 <- brewer.pal(8,"Dark2")
### WORD CLOUD #######
#######################
png("wordcloud_html_test.png", width=1280,height=800)
wordcloud(forwc2$word,forwc2$freq, scale=c(8,.5),min.freq=3,max.words=Inf, random.order=FALSE, rot.per=0, colors=pal1, vfont=c("serif","bold"))
dev.off()
我看到了一些像下面这样的其他例子来生成html格式的情节。
summary(cars)
out = plot(cars)
HTML(out, file = "testpage3.html")
所以..我尝试了很多东西,包括这样的事情......
out <- {png("wordcloud_html_test.png", width=1280,height=800)
wordcloud(forwc2$word,forwc2$freq, scale=c(8,.5),min.freq=3,max.words=Inf, random.order=FALSE, rot.per=0, colors=pal1, vfont=c("serif","bold"))
dev.off()}
HTML(out, file = "wordcloud.html")
但没有真正奏效。有人可以指导我这里缺少什么吗?
另外,我读到了(http://cran.r-project.org/doc/Rnews/Rnews_2003-3.pdf)“这不是一个真正的HTML文件,因为它甚至不包含标准标题....是否有任何软件包我可以尝试缓解这个问题?(I尝试了R studio KnitR,但我对R studio有些问题所以我放弃了。)
感谢您的指导!
答案 0 :(得分:1)
这是一个带有png文件并将其插入新html文件的函数。
#' create a html file where we insert a png image.
#' @param destdir full path to your destination html directory
#' @param pngPath full path to your origin png
#' @param htmlfile html final file
pngToHTML <- function(destdir =getwd(),
pngPath ='Rplot.png',
htmlfile='mypng.html'
){
imgdir <- "figure"
html.code <- '<!DOCTYPE html>
<html>
<head>
</head>
<body>
<img src="figure/test.png"></img>
</body>
</html>'
ll <- readLines(textConnection(html.code))
ll <- gsub("src=(.*)",paste0('src="',imgdir,'/',
basename(pngPath),'"'),ll)
imgdir=file.path(destdir,imgdir)
if (!file.exists(imgdir)) {
dir.create(imgdir)
}
else {
file.remove(list.files(imgdir, full.names = TRUE))
}
file.copy(from=pngPath,imgdir)
htmlfile=file.path(destdir,htmlfile)
cat(ll, file = htmlfile,sep='\n')
browseURL(paste("file:///", htmlfile, sep = ""))
}