简体   繁体   中英

R - Shiny widget and Leaflet not connecting

The BLschool.csv file. What I want to do is to change the map on the basis of the choice selected.

So, if the A is chosen, then the map should only show the schools with quality rating of A , likewise for B , C , D .

However, somewhere the input$schoolqual value isn't coming in, or the the data isn't being subset for some reason, I am getting this error :

Error: schqual not found

server.R

sc <- read.csv("BLschools.csv", header = TRUE, sep=",")

shinyServer(function(input, output){


  output$map <- renderLeaflet({

  schqual <- input$schoolqual %>%
  school <- subset(sc, sc$Rateoutof4 == schqual) %>%
  leaflet(data = school) %>%
  setView(lng = -73.98928, lat = 40.75042, zoom = 10) %>%
  addProviderTiles("CartoDB.Positron", options = providerTileOptions(noWrap = TRUE)) %>%
  addMarkers(clusterOptions = markerClusterOptions(~lng, ~lat), icon = greenLeafIcon, 
             popup= ~paste("<b>", as.character(school$SchoolName),"</b><br/>",
             "Rating: ", as.character(school$Rateoutof4),"<p></p>"))
  })

})

ui.R

shinyUI(
fluidPage(
   titlePanel("NYC schools"),
     sidebarLayout(
        sidebarPanel(
            selectInput(schoolqual, choices = c("A", "B","C", "D", "E"), selected = "A", label="school quality rating")),
     mainPanel(leafletOutput("map"))
   )
 )
)

Please ignore the fact that the Rateoutof4 , despite the name is in characters. I forgot to change the column name.

You can try with a reactive expression :

shinyServer(function(input, output){
  school <- reactive(subset(sc, sc$Rateoutof4 == input$schoolqual))
  output$map <- renderLeaflet({
    leaflet(data = school()) %>%
    setView(lng = -73.98928, lat = 40.75042, zoom = 10) %>%
    addProviderTiles("CartoDB.Positron", 
                     options = providerTileOptions(noWrap = TRUE)) %>%
    addMarkers(clusterOptions = markerClusterOptions(~lng, ~lat), 
               icon = greenLeafIcon, 
               popup= ~paste("<b>", 
                             as.character(school()$SchoolName),
                             "</b><br/>",
                             "Rating: ", 
                             as.character(school()$Rateoutof4),
                             "<p></p>"))
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM