[英]R - Shiny widget and Leaflet not connecting
BLschool.csv文件。 我要做的是根据所选的选择更改地图。
因此,如果选择了A
,则地图应仅显示质量等级为A
的学校,对于B
, C
, D
也是如此。
但是,在某个地方没有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.