htmlwidgets 非常有趣且十分强大,一直以来我都想使用这个包制作自己的html小部件,但是我失败了,这是我的第三次尝试,终于我成功了,hwordcloud
包出世了!这是我的第一个使用htmlwidgets编写的R包,所有希望大家容忍其中的缺陷,我会继续改进的。下面让我们来一起使用这个包吧!
首先,你可以从 github
上安装这个包:
devtools::install_github('czxa/hwordcloud')
# 或者使用git
devtools::install_git("https://github.com/czxa/hwordcloud.git")
此外,我该制作了一个简单的R shiny 方便你了解这个包:
dir <- system.file("examples", "hwordcloudC", package = "hwordcloud")
setwd(dir)
shiny::shinyAppDir(".")
祝你使用愉快!
我们使用 wordcloud2 中的数据集做演示:
标题和副标题也是可以自定义的:
一个完整的示例:
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")
hwordcloud
得益于 htmlwidgets
,你可以在 R Markdown 中使用 hwordcloud()
。
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")
hwordcloud
下面是一个非常简单的R Shiny 示例:
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)