class: center, middle, inverse, title-slide # Rendering word clouds using R + Highcharts ## ⚔
with xaringan ### ZhenXing Cheng ### 2019-02-14 --- background-image: url(https://czxa.top/images/painter.svg) --- # Introduction The [htmlwidgets](https://github.com/ramnathv/htmlwidgets) package is very interesting and powerful. All the time, I want write my own html widgets, but I failed. This is my third try, finally I success! and the `hwordcloud` package come out. As I first try 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`: ```r devtools::install_github('czxa/hwordcloud') # or just use git devtools::install_git("https://github.com/czxa/hwordcloud.git") ``` Also, I made a Shiny App for this package: ```r dir <- system.file("examples", "hwordcloud", package = "hwordcloud") setwd(dir) shiny::shinyAppDir(".") ``` Enjoy your use! --- class: inverse, center, middle # Get Started --- # A Basic example We can use [wordcloud2](https://github.com/Lchiffon/wordcloud2)'s datesets to demonstrate: ```r library(hwordcloud) library(wordcloud2) df <- demoFreq %>% head(50) hwordcloud(text = df$word, size = df$freq, width = "100%", height = "200px") ```
--- class: inverse, center, middle # Theme Parameter ### I hate complex codes, so I built some themes in this package. Just change theme parameter, you can render wordcloud in different apperance. --- # darkgreen ```r df <- wordcloud2::demoFreq %>% head(50) hwordcloud::hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "darkgreen") ```
--- # darkblue ```r df <- wordcloud2::demoFreq %>% head(50) hwordcloud::hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "darkblue") ```
--- # avocado ```r df <- wordcloud2::demoFreq %>% head(50) hwordcloud::hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "avocado") ```
--- # darkunica ```r df <- wordcloud2::demoFreq %>% head(50) hwordcloud::hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "darkunica") ```
--- # gray ```r df <- wordcloud2::demoFreq %>% head(50) hwordcloud::hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "gray") ```
--- # gridlight ```r df <- wordcloud2::demoFreq %>% head(50) hwordcloud::hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "gridlight") ```
--- # grid ```r df <- wordcloud2::demoFreq %>% head(50) hwordcloud::hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "grid") ```
--- # sandsignika ```r df <- wordcloud2::demoFreq %>% head(50) hwordcloud::hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "sandsignika") ```
--- # sunset ```r df <- wordcloud2::demoFreq %>% head(50) hwordcloud::hwordcloud(text = df$word, size = df$freq, width = "100%", height = "300px", theme = "sunset") ```
--- class: inverse, center, middle # Other Parameter ### Title and subtitle are also can be customized. --- A complete example: ```r df <- wordcloud2::demoFreq %>% head(50) hwordcloud::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") ```
--- class: inverse center middle # R Markdown Usage --- ## Use `hwordcloud` in R Markdown Benifit 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. ```r df <- wordcloud2::demoFreq %>% head(50) hwordcloud::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") ``` --- class: inverse center middle # Shiny Usage --- ## Use `hwordcloud` in R Shiny Here is a very simple shiny example: ```r library(shiny) ui <- fluidPage( titlePanel("Highcharter word cloud example"), mainPanel( hwordcloud::hwordcloudOutput("shinytest", height = "500px") ) ) server <- function(input, output) { library(test) library(wordcloud2) df <- demoFreq %>% head(50) output$shinytest <- hwordcloud::renderHwordcloud({ hwordcloud::hwordcloud(text = df$word, size = df$freq) }) } shinyApp(ui = ui, server = server) ```