0

I have a data frame as follows:

d <- data_frame(number = 1:4, 
        date = c(1600, 1700, 1800, 1900),
        T_name = c("name1", "name2", "name3", "name4"),
        casualties= c(18, 89, 97, 34),
        m_name= c("name1", "name2", "name3", "name4"),
        longitude = c(87.7, 103.2, 67.0, 76.8),
        latitude = c(23.4, 78.3, 78.9, 34.9))

I'm trying to get my map to react to a select input but when running the code I get the following error message:

Length of logical index vector must be 1 or 9 (the number of columns), not 25

There are 9 columns in my real data, and 25 rows, so this does make some sense. I just can't figure out how to amend my code. What I want is a drop down list sorted by unique T_name, and when a new T_name is selected the map updates with only data from that T_name.

UI

library(shiny)
library(leaflet)

ui <- {fluidPage("app title", 
         sidebarLayout(
           sidebarPanel(
             selectInput(inputId = "input1", label = "Name" ,choices = 
                           unique(d$T_name))

           ),

           mainPanel(
             leafletOutput("mymap"))
         )
)}

server

server <- function(input, output) {
  react <- reactive({
  req(input$input1)
df <- d[d$T_name == input$input1]
df
  })
output$mymap <- renderLeaflet({ req(input$input1)

leaflet() %>% addTiles() %>% setView(lng = -100.94, lat = 38.94 , zoom 
= 3.5) %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap) %>%
  addMarkers(lng=react()$longitude, lat=react()$latitude,
             popup = paste(d$m_name)
  )
  })
}

run app

shinyApp(ui, server)

I've been playing around with this for days so any help is greatly appreciated!

Mrmoleje
  • 453
  • 1
  • 12
  • 35
  • First, you should include libraries necessary to run your app. You have a few typos in your code and what are you trying to achieve with this `df <- d[d$T_name == input$input1 & d$colonialist_casualties]`? – MLavoie May 27 '18 at 08:28
  • @MLavoie Okay libraries are in there now. I’ve amended the above chunk of code. My understanding is that this links the select input to the map. So when a T_name is selected, only data with that T_name are incorporated into the map – Mrmoleje May 27 '18 at 09:07
  • @MLavoie that helps, as in the map now runs. But when I select "name2" from the drop down list the pin stays on "name1", i.e. the data are not updated in the map – Mrmoleje May 27 '18 at 10:45
  • Sorry I meant `df <- d[d$T_name == input$input1, ]`. I wanted to put emphasis on the missing comma. – MLavoie May 27 '18 at 10:50
  • @MLavoie yes that works, thank you so much. The only issue is this: The `popup = paste(d$m_name)` bit of code isn't working as the m_name in the popup stays the same when I select a new T_name. I would have thought this would update as the coordinates update? – Mrmoleje May 27 '18 at 11:04
  • Try this: `leaflet(data = react()) %>% addTiles() %>% setView(lng = -100.94, lat = 38.94 , zoom = 6) %>% addProviderTiles(providers$Esri.NatGeoWorldMap) %>% addMarkers(lng = ~longitude, lat= ~latitude, popup = ~m_name )`. – MLavoie May 27 '18 at 11:14
  • Yeah that works, thank you much. I understand what you're doing there as well so not just blindly copying. – Mrmoleje May 27 '18 at 11:21

0 Answers0