BLschool.csv 文件。 我想做的是根据所选的选择更改地图。
因此,如果选择
A
,则地图应仅显示质量评级为 A
的学校,同样适用于 B
、C
、D
。
但是,在某个地方
input$schoolqual
值没有传入,或者由于某种原因数据不是子集,我收到此错误:
Error: schqual not found
服务器.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"))
)
)
)
请忽略
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>"))
})
})