# Module UI
#' @title mod_yearly_chart_ui and mod_yearly_chart_server
#' @description A shiny Module.
#'
#' @param id shiny id
#' @param input internal
#' @param output internal
#' @param session internal
#'
#' @rdname mod_yearly_chart
#'
#' @keywords internal
#' @export
#' @importFrom shiny NS tagList
mod_yearly_chart_ui <- function(id){
data <- readRDS("healthexp.Rds")
data$Region <- as.factor(data$Region)
ns <- NS(id)
xlim <- list(
min = min(data$Health.Expenditure) - 500,
max = max(data$Health.Expenditure) + 500
)
ylim <- list(
min = min(data$Life.Expectancy),
max = max(data$Life.Expectancy) + 3
)
fluidPage(
# This line loads the Google Charts JS library
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'}"
),
h2("Google Charts demo"),
googleBubbleChart(ns("chart"),
width="100%", height = "475px",
# Set the default options for this chart; they can be
# overridden in server.R on a per-update basis. See
# https://developers.google.com/chart/interactive/docs/gallery/bubblechart
# for option documentation.
options = list(
fontName = "Source Sans Pro",
fontSize = 13,
# Set axis labels and ranges
hAxis = list(
title = "Health expenditure, per capita ($USD)",
viewWindow = xlim
),
vAxis = list(
title = "Life expectancy (years)",
viewWindow = ylim
),
# The default padding is a little too spaced out
chartArea = list(
top = 50, left = 75,
height = "75%", width = "75%"
),
# 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 = 16
),
tooltip = list(
textStyle = list(
fontSize = 12
)
)
)
),
fluidRow(
shiny::column(4, offset = 4,
sliderInput(ns("year"), "Year",
min = min(data$Year), max = max(data$Year),
value = min(data$Year), animate = TRUE)
)
)
)
}
# Module Server
#' @rdname mod_yearly_chart
#' @export
#' @keywords internal
mod_yearly_chart_server <- function(input, output, session){
data <- readRDS("healthexp.Rds")
data$Region <- as.factor(data$Region)
ns <- session$ns
# Provide explicit colors for regions, so they don't get recoded when the
# different series happen to be ordered differently from year to year.
# http://andrewgelman.com/2014/09/11/mysterious-shiny-things/
defaultColors <- c("#3366cc", "#dc3912", "#ff9900", "#109618", "#990099", "#0099c6", "#dd4477")
series <- structure(
lapply(defaultColors, function(color) { list(color=color) }),
names = levels(data$Region)
)
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 region
# so that Google Charts orders and colors the regions
# consistently.
df <- data %>%
filter(Year == input$year) %>%
select(Country, Health.Expenditure, Life.Expectancy,
Region, Population) %>%
arrange(Region)
})
output$chart <- reactive({
# Return the data and options
list(
data = googleDataTable(yearData()),
options = list(
title = sprintf(
"Health expenditure vs. life expectancy, %s",
input$year),
series = series
)
)
})
}
## To be copied in the UI
# mod_yearly_chart_ui("yearly_chart_ui_1")
## To be copied in the server
# callModule(mod_yearly_chart_server, "yearly_chart_ui_1")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.