knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
The htmlwidgets package is very interesting and powerful. All the time, I want to write my own html widgets, but I failed. This is my third try, finally I success! As I first try of using htmlwidgets
, I hope you will tolerate some of the flaws of this R package, and I will improve it in the future. Next, let's use hwordcloud
!
First, you can install it from github
:
devtools::install_github('czxa/hwordcloud') # or just use git devtools::install_git("https://github.com/czxa/hwordcloud.git")
Also, I made a shiny application example for this package:
dir <- system.file("examples", "hwordcloud", package = "hwordcloud") setwd(dir) shiny::shinyAppDir(".")
Enjoy your use!
We can use wordcloud2's datesets to demonstrateļ¼
library(hwordcloud) library(wordcloud2) df <- head(demoFreq, 50) hwordcloud(text = df$word, size = df$freq, width = "100%", height = "200px")
I hate complex codes, so I built some themes in this package. Just change theme parameter, you can render wordcloud in different apperance.
hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "darkgreen")
hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "darkblue")
hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "avocado")
hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "darkunica")
hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "gray")
hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "gridlight")
hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "grid")
hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "sandsignika")
hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "sunset")
Title and subtitle are also can be customized.
A complete example:
hwordcloud(text = df$word, size = df$freq, width = "100%", height = "200px", theme = "sunset", title = "Word Cloud", titleAlign = "center", titleColor = "#333333", titleSize = "20px", subtitle = "czxa.top", subtitleColor = "#666666", subtitleAlign = "center", subtitleSize = "16px")
hwordcloud
within RMarkdown documentsBenefit from htmlwidgets
, you can use hwordcloud()
function in R Markdown document. For example, you can create a github document and code following codes in it, save it as a .Rmd
documents, then knit
it, you will find a word cloud embedded in it.
hwordcloud(text = df$word, size = df$freq, width = "100%", height = "400px", theme = "sunset", title = "Word Cloud", titleAlign = "center", titleColor = "#333333", titleSize = "20px", subtitle = "czxa.top", subtitleColor = "#666666", subtitleAlign = "center", subtitleSize = "16px")
hwordcloud
within Shiny applicationsHere is a very simple shiny example:
library(shiny) library(wordcloud2) ui <- fluidPage( titlePanel("word cloud example"), mainPanel( hwordcloudOutput("shinytest", height = "500px") ) ) server <- function(input, output) { df <- head(demoFreq, 50) output$shinytest <- renderHwordcloud({ hwordcloud(text = df$word, size = df$freq) }) } shinyApp(ui = ui, server = server)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.