2013-10-15 6 views
0

Salut experts R là,exportation Png en HTML en utilisant R2HTML

J'ai un nuage de mot, mais je voulais en faire un fichier html, pour que d'autres personnes peuvent utiliser cette wordcloud dans leur site Web. Donc .. J'ai essayé R2HTML mais j'ai besoin de quelques conseils pour traiter le fichier png. Voici mon code:

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() 

je vis un autre exemple comme ci-dessous pour tracer la courbe au format html.

summary(cars) 
out = plot(cars) 
HTML(out, file = "testpage3.html") 

Alors .. J'ai essayé un tas de choses, y compris quelque chose comme ça ..

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") 

mais rien de vraiment travaillé. Quelqu'un peut-il me guider ce qui manque ici?

En outre, j'ai lu que (http://cran.r-project.org/doc/Rnews/Rnews_2003-3.pdf) "ce n'est pas un vrai fichier HTML, car il ne contient même pas d'en-têtes standard .... Y at-il un paquet que je peux essayer d'atténuer cela? KnitR mais j'ai un problème avec le studio de R, donc j'ai abandonné.)

Merci pour les conseils!

Répondre

1

Voici une fonction qui prend un fichier .png et l'insérer dans un nouveau fichier 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 = "")) 
} 
+0

Merci beaucoup! Donc, si je veux que quelqu'un puisse voir ce fichier html sur son bureau, est-ce que signifie que je dois connaître son chemin d'accès au répertoire destination html et le chemin d'accès au fichier png? – user1486507

+0

@ user1486507 Non, vous générez le fichier html où vous le souhaitez. Que vous devriez envoyer le résultat (fichier + dossier de la figure) comme un fichier compressé. – agstudy

+0

Génial. Merci!! C'est la deuxième fois que vous m'aidiez! =) – user1486507

Questions connexes