将Shiny与Leaflet整合在一起

时间:2016-05-23 08:40:12

标签: r shiny leaflet

我正在尝试想象墨尔本市中心的所有树木。 我正在使用的数据集可在此处获取 - Urban Forest Data

我成功地绘制了数据集中存在的所有树,并根据他们的预期寿命对它们进行了颜色编码。

我想知道如何将它与Shiny整合在一起,以便我可以通过“Precinct”列过滤该图。也就是说,当我选择“CBD”时,它应该只绘制该区域中的树。到目前为止,我的代码和工作图的屏幕截图如下:

代码:

library(leaflet)
library(dplyr)
library(readr)
td <- read.csv("treedata.csv", header = TRUE)

pal <- colorNumeric(
  palette = "RdYlGn",
  domain = td$LifeExpectencyValue
)

leaflet(td) %>% addTiles(
  urlTemplate = 'http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png',
  attribution='Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
) %>% addCircleMarkers(radius= 5,fillOpacity = 0.5, stroke = FALSE,color=~pal(LifeExpectencyValue),
                                                popup=paste("Name:", td$CommonName, "<br>", "Years Left:", td$LifeExpectency, "<br>", "Genus:", td$Genus)

) %>% addLegend(pal = pal, values = ~LifeExpectencyValue, opacity = 1, title = "Life Expectancy")

情节的截图: enter image description here

我是Shiny的新手所以非常感谢任何帮助。

更新

尝试了闪亮的代码:

require(rCharts)
library(shiny)

ui <- fluidPage(
  selectInput("precinct",
              label="Precinct",
              choices = sort(td$Precinct),
              selected = "CBD"),
  plotOutput("treedat") #Giving an input name and listing out types to choose in the Shiny app
)

server <- function(input, output){

  output$treedat <- renderLeaflet({
    PRECINCT = input$precinct
    precinct = subset(td, precinct == PRECINCT)
    td2 <- leaflet(td) %>% addTiles(
      urlTemplate = 'http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png',
      attribution='Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
    ) %>% addCircleMarkers(radius= 5,fillOpacity = 0.5, stroke = FALSE,color=~pal(LifeExpectencyValue),
                           popup=paste("Name:", td$CommonName, "<br>", "Years Left:", td$LifeExpectency, "<br>", "Genus:", td$Genus)

    ) %>% addLegend(pal = pal, values = ~LifeExpectencyValue, opacity = 1, title = "Life Expectency")
    return(td2)
  })
}
shinyApp(ui = ui, server = server)

收到错误,说找不到对象'Precinct'。

1 个答案:

答案 0 :(得分:2)

几点

  1. 您在代码中使用leaflet,但在应用中使用rCharts。在这里,我只是在闪亮的应用中使用leaflet。 (而且,据我所知rCharts还没有CRAN?)

  2. 填充下拉列表的选项取决于数据。因此,我已使用selectInputrenderUI/UIOutput移至服务器中。我还在选项周围使用as.chacter,因此您可以看到字符表示,而不是factor级别

  3. 我还删除了为树木着色的代码,因为原始数据中没有名为 LifeExpectancy 的列(我假设您已清除列标题)某处?)

    最后,对您的数据进行子集化方式进行了一些小的更改,您就可以了。

    library(leaflet)
    library(shiny)
    
    ui <- fluidPage(
      uiOutput("precinct"),
      leafletOutput("treedat") #Giving an input name and listing out types to choose in the Shiny app
    )
    
    server <- function(input, output){
    
      # td <- read.csv("~/Desktop/Melbourne_s_Urban_Forest_Tree_data.csv", header = T)
    
      output$precinct <- renderUI({
    
        choices <- as.character(unique(td$Precinct))  
        choices <- c('All', choices)
        selectInput(inputId = "precinct", label = "Precinct", choices = choices, selected = "CBD")
    
      })
    
    
      output$treedat <- renderLeaflet({
    
        ## get the choice from teh drop-down box
        PRECINCT = input$precinct
    
        ## supbset the data based on the choice
        if(PRECINCT != 'All'){
            td2 <- td[td$Precinct == PRECINCT, ]
        }else{
            td2 <- td
        }
        ## plot the subsetted ata
        td2 <- leaflet(td2) %>% addTiles(
          urlTemplate = 'http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png',
          attribution='Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>') %>% 
          addCircleMarkers(radius= 5,fillOpacity = 0.5, stroke = FALSE)
        return(td2)
      })
    }
    
    shinyApp(ui = ui, server = server)