【问题标题】:R - Shiny widget and Leaflet not connectingR - 闪亮的小部件和传单没有连接
【发布时间】:2016-11-15 07:46:32
【问题描述】:

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,尽管名称是字符。我忘了更改列名。

【问题讨论】:

  • 从前 2 行中删除 `%>%`
  • @HubertL 删除它们 - 地图显示为空白,没有任何标记
  • 尝试让你的代码首先在 R 中运行(在闪亮的应用程序之外),这样更容易调试
  • @HubertL,是的,我已经做到了,schqual &lt;- "A" 和 school &lt;- subset(sc, sc$Rateoutof4 == schqual) 然后运行应用程序,它会显示 A 评级的学校。
  • 您是否尝试点击“在浏览器中打开”?

标签: r shiny leaflet


【解决方案1】:

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

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

【讨论】:

  • 嘿!它起作用了,顺便说一句,将schqual 更改为schoolqual,因为您删除了schqual &lt;- input$schoolqual
猜你喜欢
  • 1970-01-01
  • 2021-01-14
  • 2018-07-25
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 2014-08-26
相关资源
最近更新 更多