-
Notifications
You must be signed in to change notification settings - Fork 47
ECharts2Shiny: A New Solution for Interactive Word Cloud in Shiny Applications
ECharts2Shiny is an R package helping insert interactive charts into Shiny Applications. It's using the javascript library ECharts developed by Baidu team. Currently, ECharts2Shiny supports eight types of charts, including
- Pie charts
- Line charts
- Bar charts
- Scatter plots
- Radar chart
- Gauge
- Word Cloud
- Heat Map
However, in this wiki, we would only cover the Word Cloud feature. For other charts, you may want to refer to ECharts2Shiny examples.
ECharts2Shiny can accept two types of data input for plotting, vector and data.frame.
Essentially, word cloud is using the frequency of the elements in the data. If we directly give a vector as input, ECharts2Shiny will generate the frequency automatically. For example, you can enter renderWordcloud(div_id = 'test', data = c('A', 'A', 'A', 'B', 'B', 'C', 'A'))
and the function will process the data and know the frequency of each unique element in the data.
A full example is as below
library(shiny)
library(ECharts2Shiny)
sample_data_for_wordcloud <- c(rep("R", 100),
rep("Python", 100),
rep("SAS", 90),
rep("VBA", 50))
# Server function -------------------------------------------
server <- function(input, output) {
renderWordcloud("test", data =sample_data_for_wordcloud)
}
# UI layout -------------------------------------------------
ui <- fluidPage(
# We MUST load the ECharts javascript library in advance
loadEChartsLibrary(),
tags$div(id="test", style="width:100%;height:500px;"),
deliverChart(div_id = "test")
)
# Run the application --------------------------------------
shinyApp(ui = ui, server = server)
Users can also use a data.frame as the input for function renderWordcloud
. Please note that the data.frame must be made up of two columns, 'name' and 'value'. The 'name' column is the names of elements, and 'value' column is the frequencies of these elements. The "value" column must be numeric or integer.
A full example is as belo
library(shiny)
library(ECharts2Shiny)
sample_data_for_wordcloud <- data.frame(name = c("Asia", "Africa", "North America", "South America",
"Antarctica", "Europe", "Australia"),
value = c(44391162, 30244049, 24247039, 17821029, 14245000,
10354636, 7686884))
# Server function -------------------------------------------
server <- function(input, output) {
renderWordcloud("test", data =sample_data_for_wordcloud)
}
# UI layout -------------------------------------------------
ui <- fluidPage(
# We MUST load the ECharts javascript library in advance
loadEChartsLibrary(),
tags$div(id="test", style="width:100%;height:500px;"),
deliverChart(div_id = "test")
)
# Run the application --------------------------------------
shinyApp(ui = ui, server = server)
The default shape of the word cloud delivered by ECharts2Shiny is 'circle'. However, you can also have your word cloud with other shapes, including
- "circle" (default value)
- "cardioid"
- "diamond"
- "triangle-forward"
- "triangle"
- "pentagon"
- "star"
The only extra work you need to do is to specify the shape
argument, like shape = "diamond"
. You may also refer to a full example.
Another two arguments, sizeRange
and rotationRange
, are also useful in order to customize your word cloud charts.
sizeRange
is the font size range in the word cloud. It should be a vector of length two, like c(15, 50)
. These two numbers would be the font sizes of the two elements with the lowest or highest frequencies.
rotationRange
is the rotation angle range in the word cloud. It should be a vector of length two as well, like c(-45, 45)
. All elements in the word cloud would rotate randomly within this given range.
Maybe call me XD ;-)