Привет, эксперты 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 файл, поскольку он даже не содержит стандартных заголовков... там какой-нибудь пакет, который я могу попытаться смягчить? (Я попробовал R studio KnitR, но у меня есть некоторые проблемы с R-студией, поэтому я сдался.)
Спасибо за руководство!
Здесь функция, которая принимает файл 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 = ""))
}