Skip to content

Instantly share code, notes, and snippets.

@alexeyknorre
Created January 30, 2018 07:14
Show Gist options
  • Save alexeyknorre/af91c4b04b110dbfbaa4271250ad529b to your computer and use it in GitHub Desktop.
Save alexeyknorre/af91c4b04b110dbfbaa4271250ad529b to your computer and use it in GitHub Desktop.
Shiny demo: Russian crime statistics
library(shiny)
library(ggvis)
df <- read.csv("https://github.com/alexeyknorre/workshop_undone_2018/raw/master/murder_crimes_by_regions.csv",
encoding = "CP1251",
stringsAsFactors = F)
shinyServer(function(input, output) {
region_popup <- function(x) {
region <- df[df$region == x$region, ]
paste0("<b>", as.character(region$region), "</b><br>",
"Murders and murder attempts count: ",region$crimes, "<br>",
"Population: ", format(region$population,
big.mark = " ",
scientific = FALSE)," people<br>",
"Area of the region: ", format(region$area,
big.mark = " ",
scientific = FALSE), " sq.km."
)
}
df %>%
ggvis(~crimes, ~population, key := ~region) %>%
layer_points() %>%
add_tooltip(region_popup, "hover") %>%
add_axis("x", title = "Murders and murder attempts count") %>%
add_axis("y", title = "Population",title_offset = 80) %>%
bind_shiny("plot_1")
df %>%
ggvis(~crimes / population * 100000, ~area, key := ~region) %>%
layer_points() %>%
add_tooltip(region_popup, "hover") %>%
add_axis("x", title = "Murders and murder attempts per 100k people") %>%
add_axis("y", title = "Area of the region, sq.km.",title_offset = 80) %>%
bind_shiny("plot_2")
})
library(shiny)
library(ggvis)
shinyUI(fluidPage(
titlePanel("Demo: murders in Russian regions"),
div(
conditionalPanel(
condition = "input.col == 'Regular plot'", ggvisOutput("plot_1")),
conditionalPanel(
condition = "input.col == 'Standartized plot'", ggvisOutput("plot_2")),
align = "center"),
hr(),
div(
radioButtons("col","Switcher",
choices = c("Regular plot", "Standartized plot"),
selected = "Regular plot"),
align = "center")
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment