Choropleth

Arc

Bubbles

Datamaps proxy!
by John Coene.

show with app
library(shiny)
library(datamaps)

cn <- unique(countrycode::codelist$iso3c)
cn <- cn[!is.na(cn)]

data <- data.frame(name = cn, color = ceiling(runif(length(cn), 1, 50)))

coords <- data.frame(city = c("London", "New York", "Beijing", "Sydney"),
                     lon = c(-0.1167218, -73.98002, 116.3883, 151.18518),
                     lat = c(51.49999, 40.74998, 39.92889, -33.92001),
                     values = 1:4)

ui <- fluidPage(
  fluidRow(
    column(
      4,
      h3("Choropleth"),
      selectInput(
        "countrySelect",
        "Select Country",
        choices = cn,
        selected = "RUS"
      ),
      sliderInput(
        "value",
        "Value",
        min = 1,
        max = 50,
        value = 20
      ),
      actionButton("update", "Update")
    ),
    column(
      4,
      h3("Arc"),
      selectInput(
        "from",
        "From:",
        choices = cn,
        selected = sample(cn, 1)
      ),
      selectizeInput(
        "to",
        "To:",
        choices = cn,
        selected = sample(cn, 1)
      ),
      actionButton(
        "submit",
        "Add arc"
      ) 
    ),
    column(
      4,
      h3("Bubbles"),
      numericInput(
        "lon",
        "Longitude",
        value = 50
      ),
      numericInput(
        "lat",
        "Latitude",
        value = 50
      ),
      textInput(
        "city",
        "City",
        value = "City"
      ),
      sliderInput(
        "value",
        "Value",
        min = 1,
        max = 4,
        step = 1,
        value = 3
      ),
      actionButton(
        "sub",
        "Submit"
      )
    )
  ),
  fluidRow(
    datamapsOutput("map")
  )
)

server <- function(input, output){
  
  arc <- reactive({
    data.frame(from = input$from, to = input$to)
  })
  
  updated_data <- reactive({
    data.frame(name = input$countrySelect, value = input$value)
  })
  
  update <- reactive({
    df <- data.frame(city = input$city, lon = input$lon, lat = input$lat, values = input$value)
    rbind.data.frame(coords, df)
  })
  
  output$map <- renderDatamaps({
    data %>%
      datamaps(responsive = TRUE) %>%
      add_choropleth(name, color) %>% 
      add_data(coords) %>% 
      add_bubbles(lon, lat, values * 2, values, city)
  })
  
  observeEvent(input$update, {
    datamapsProxy("map") %>%
      add_data(updated_data()) %>% # pass updated data
      update_choropleth(name, value, TRUE) # update
  })
  
  observeEvent(input$submit, {
    datamapsProxy("map") %>%
      add_data(arc()) %>%
      update_arcs_name(from, to)
  })
  
  observeEvent(input$sub, {
    datamapsProxy("map") %>%
      add_data(update()) %>% # pass updated data
      update_bubbles(lon, lat, values * 2, values, city) # update
  })
}

shinyApp(ui, server)
Code license: GPL-3