Skip to content

Latest commit

 

History

History
170 lines (122 loc) · 5.03 KB

InteractiveMapInR.MD

File metadata and controls

170 lines (122 loc) · 5.03 KB

Lab: Creating an Interactive Map in R

Welcome to the 2nd RStudio lab, in which we create an interactive map using two libraries. Shiny, which is an amazing library for prototyping interactive user interfaces in R and Leaflet which allows us to use OpenStreetMap data to create an interactive map. Shiny will use Leaflet as one of it’s UI components (like any other UI component like button, slider or text field).

  1. Install the following packages, you can do so by simply paste and execute the following command into the bottom left Console window
install.packages("shiny")

install.packages("leaflet")

  1. Paste the following code into a new R edit window in RStudio:
library(shiny)
library(leaflet)

r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()

ui <- fluidPage(
        leafletOutput("mymap"),
        p(),
        actionButton("recalc", "New points")
    )

server <- function(input, output, session) {
            points <- eventReactive(input$recalc, {
                points = cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)

                #Insert code here 

                return(points)}, ignoreNULL = FALSE)

        observeEvent(input$Map_shape_click, { # update the location selectInput on map clicks
            output$coordinates <- renderText({ 
                "You have selected this"
            })
            })
        output$mymap <- renderLeaflet({
            leaflet() %>%
            addProviderTiles(providers$Stamen.TonerLite,
            options = providerTileOptions(noWrap = TRUE)
            ) %>%
            addMarkers(data = points())
        })

}

shinyApp(ui, server)
  1. Click on the Run App button

You’ll see the following application appear:

  1. If you click New points, a new set of random points will be generated and displayed.

  2. Now let's change the code to display the list of points on the user interface.

  3. Then, it is time to also change the behavior of this application by changing the “server” object. Therefore, please add the following three lines to the so-called “eventhandler” responsible to execute code if the button is clicked:

Replace the code here under


ui <- fluidPage(	
  leafletOutput("mymap"),	
  p(),	
  actionButton("recalc", "New points")	
)

with

ui <- fluidPage(
  leafletOutput("mymap"),
  p(),
  actionButton("recalc", "New points"),
  p(),
  textOutput("coordinates")
)

and also replace

points <- eventReactive(input$recalc, {	
    points = cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)	
    return(points)	
  }, ignoreNULL = FALSE)

with

points <- eventReactive(input$recalc, {    
    points = cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)    
    output$coordinates <- renderText({       
      points      
    })    
    return(points)    
  }, ignoreNULL = FALSE)

such that the complete code section looks like this:

library(shiny)

library(leaflet)

r_colors <- rgb(t(col2rgb(colors()) / 255))

names(r_colors) <- colors()

ui <- fluidPage(  
  leafletOutput("mymap"),  
  p(),  
  actionButton("recalc", "New points"),  
  p(),  
  textOutput("coordinates")  
)

server <- function(input, output, session) {
  
  points <- eventReactive(input$recalc, {    
    points = cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)    
    output$coordinates <- renderText({       
      points      
    })    
    return(points)    
  }, ignoreNULL = FALSE)
    
  observeEvent(input$Map_shape_click, { # update the location selectInput on map clicks    
    output$coordinates <- renderText({       
      "You have selected this"      
    })    
  })
  
  output$mymap <- renderLeaflet({    
    leaflet() %>%      
      addProviderTiles(providers$Stamen.TonerLite,                       
                       options = providerTileOptions(noWrap = TRUE)                       
      ) %>%      
      addMarkers(data = points())    
  })  
}

shinyApp(ui, server)
  1. If you now re-run this application, you’ll notice a list of coordinate pairs which updates every time you click the New points button

This concludes this lab, we hope that you had fun! If you want to know more about Shiny and Leaflet, please visit the following links:

https://shiny.rstudio.com/

https://rstudio.github.io/leaflet/