Shiny is an R package that makes it easy to build interactive web apps straight from R. You can host standalone apps on a webpage or embed them in R Markdown documents or build dashboards. (R Studio)
This one below is an example of how useful Shiny for data visualisation:
Codes for the above visualisation:
library(shiny)
library(googleCharts)
library(dplyr)
library(readxl)
head(data)
## # A tibble: 6 x 6
## woe_label Island year Grdpc HDI Population
## <fct> <fct> <dbl> <dbl> <dbl> <dbl>
## 1 Aceh Sumatera 2000 4995. 65.3 3930905
## 2 North Sumatra Sumatera 2000 5848. 66.6 11649655
## 3 West Sumatra Sumatera 2000 5388. 65.8 4248931
## 4 Riau Sumatera 2000 5746. 67.3 4957627
## 5 Jambi Sumatera 2000 3503. 65.4 2413846
## 6 South Sumatra Sumatera 2000 4506. 63.9 6899675
xlim <- list(
min = 1000,
max = 160000
)
ylim <- list(
min = 50,
max = 85
)
ui <- fluidPage(
googleChartsInit(),
# Use the Google webfont "Source Sans Pro"
tags$link(
href=paste0("http://fonts.googleapis.com/css?",
"family=Source+Sans+Pro:300,600,300italic"),
rel="stylesheet", type="text/css"),
tags$style(type="text/css",
"body {font-family: 'Source Sans Pro'}"
),
googleBubbleChart("chart",
width="100%", height = "420px",
# Set the default options for this chart; they can be
# overridden in server.R on a per-update basis.
options = list(
fontName = "Source Sans Pro",
fontSize = 15,
# Set axis labels and ranges
hAxis = list(
scaleType = "log",
title = "GRDP per Capita per Year (RP 000)",
viewWindow = xlim
),
vAxis = list(
title = "Human Development Index (HDI)",
viewWindow = ylim
),
# The default padding is a little too spaced out
chartArea = list(
top = 50, left = 75,
height = "75%", width = "85%"
),
# Allow pan/zoom
explorer = list(),
# Set bubble visual props
bubble = list(
opacity = 0.4, stroke = "none",
# Hide bubble label
textStyle = list(
color = "none"
)
),
# Set fonts
titleTextStyle = list(
fontSize = 20
),
tooltip = list(
textStyle = list(
fontSize = 12
)
)
)
),
fluidRow(
shiny::column(2, offset = 1,
selectInput("Island", "Island",
c("All",unique(as.character(data$Island))))),
shiny::column(3, offset = 0.5,
sliderInput("year", "Year",
min = 2004, max = max(data$year),
value = 1, animate = TRUE)),
shiny::column(5, offset = 0.5,
h2("A region scores higher HDI when the lifespan is higher, the education level is higher, and the GRDP per capita is higher. However, having high GRDP per capita will not guarantee a region can score high HDI."))
)
)
server <- function(input, output, session) {
defaultColors <- c("#3366cc", "#dc3912", "#ff9900", "#109618", "#990099", "#0099c6")
series <- structure(
lapply(defaultColors, function(color) { list(color=color) }),
names = levels(data$Island)
)
yearData <- reactive({
# Filter to the desired year, and put the columns
# in the order that Google's Bubble Chart expects
# them (name, x, y, color, size). Also sort by Island
# so that Google Charts orders and colors the Island
# consistently.
if (input$Island != "All") {
df <- data %>%
filter(year == input$year, Island== input$Island) %>%
select(woe_label, Grdpc, HDI,
Island, Population) %>%
arrange(Island)
}
else
df <- data %>%
filter(year == input$year) %>%
select(woe_label, Grdpc, HDI,
Island, Population) %>%
arrange(Island)
})
output$chart <- reactive({
# Return the data and options
list(
data = googleDataTable(yearData()),
options = list(
title = sprintf(
"GRDP per Capita versus HDI in Indonesia, %s",
input$year),
series = series
)
)
})
}
shinyApp(ui, server)
Comments
Post a Comment