Learn R Programming

datamaps (version 0.0.3)

update_bubbles: Dynamically add bubbles

Description

Dynamically add bubble using Shiny.

Usage

update_bubbles(proxy, lon, lat, radius, color, name, ...)

Arguments

proxy

a proxy as returned by datamapsProxy.

lon, lat

coordinates of bubbles.

radius

radius of bubbles.

color

color of bubbles.

name

name of bubbles.

...

any other variable to use in tooltip.

Examples

Run this code
# NOT RUN {
library(shiny)

ui <- fluidPage(
 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"
 ),
 datamapsOutput("map")
)

server <- function(input, output){

  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)

  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({
    coords %>%
      datamaps() %>%
      add_bubbles(lon, lat, values * 2, values, city)
  })

  observeEvent(input$sub, {
    datamapsProxy("map") %>%
      add_data(update()) %>% # pass updated data
      update_bubbles(lon, lat, values * 2, values, city) # update
  })

}

shinyApp(ui, server)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab