1
votes

I want to create a map with a sidebar where you can select the range of a time period. The idea is that the map shows marks only for hotels which opened during the period. I had no clue to do that so I tried to use selectInput instead since it seemed a little easier for me. But it seems I still can't make it work. Any guidance would be appreciated.

Here's the sample data frame:

Hotel Year   lat        long
  A   2000 41.886337 -87.628472
  B   2005 41.88819  -87.635199
  C   2010 41.891113 -87.63301

Here's the ui.R:

#ui.R
library(shiny)
library(leaflet)

shinyUI(fluidPage(
  titlePanel("Hotel Map"),
  sidebarLayout(
    sidebarPanel(
      selectInput("year", 
                  label = "Choose Year:",
                  choices = c(2000,2005,2010),
                  selected = 2000
      )),

    mainPanel (leafletOutput("map","100%",300))
  )
))

Here's the server.R:

library(shiny)
library(leaflet)
source("RStudio\\Map-app")
hotels <- read.csv("RStudio\\Map-app\\ChicagoHotels.csv")

shinyServer(
  function(input, output) {

    output$map <- renderLeaflet({
    df <- hotels[hotels$Year == input$year,]  
    leaflet()  %>%
        addTiles() %>%
        addCircles(data = df)
    })

  }
    )
2
What code do you have so far? - alistaire
SO is not a code-writing service. There are already plenty of examples on SO with leaflet/shiny - MLavoie
@alistaire & @ Mlavoie Thank you for your comments. I revised my question. If you can make any suggestions, it would be very much appreciated. - Yoshi

2 Answers

2
votes

@InfiniteFlashChess That´s correct.

Server.R

library(leaflet)

hotels <- read.table(text = "Hotel Year  latitude        longitude
                              A   2000  41.886337      -87.628472
                              B   2005  41.88819       -87.635199
                              C   2010  41.891113      -87.63301", 
                     header = TRUE)

shinyServer(function(input, output) {

output$map <- renderLeaflet({
  df <- hotels[hotels$Year == input$year,]  
  leaflet()  %>%
    addTiles() %>%
    addMarkers(data = df)
 })

})

Ui.R

library(shiny)
library(leaflet)

shinyUI(fluidPage(
titlePanel("Hotel Map"),
 sidebarLayout(
  sidebarPanel(
   selectInput("year", 
               label = "Choose Year:",
               choices = c(2000,2005,2010),
               selected = 2000
  )),

mainPanel (leafletOutput("map","100%",300))

              )
))
0
votes

Well first of all, something you should look to undersand is what the mainPanel is doing in the first place. The mainPanel will NOT take in

leafletMap("map","100%",300)

It requires you to input this instead:

leafletOutput("map", "100%", 300)

or some other related leafletOuput statement.

The way shiny is designed, if you want graphs or tables to output, you need to have the output$map object you've created in the server.R file, come back to the ui.R and your graph will only output based off a specifc output statement.

This page has examples and help galore.

Leaflet link you should really look at, so click me!

Thank you for introducing me to leaflet by the way, this is a much better tool than the googleVis package's gvisGeoMap feature.