繁体   English   中英

R-闪亮的小部件和小叶未连接

[英]R - Shiny widget and Leaflet not connecting

BLschool.csv文件。 我要做的是根据所选的选择更改地图。

因此,如果选择了A ,则地图应仅显示质量等级为A的学校,对于BCD也是如此。

但是,在某个地方没有input$schoolqual值,或者由于某种原因数据不是子集,我得到了这个错误:

Error: schqual not found

服务器

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>"))
  })

})

用户界面

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"))
   )
 )
)

尽管名称是字符,但请忽略Rateoutof4的事实。 我忘了更改列名。

您可以尝试使用反应式表达式:

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>"))
  })
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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