I'm trying to visualise all the trees present in Central Melbourne. The dataset I'm using is available here - Urban Forest Data
I'm successful in plotting all the trees present in the dataset and also colour coded them according to their Life Expectancy.
I was wondering on how I can integrate this with Shiny so that I can filter the plot by the column "Precinct". That is, when I select "CBD", it should plot only trees in that area. My code so far and a screenshot of the working plot is as below:
Code:
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> — Map data © <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")
I'm new to Shiny so any help here is deeply appreciated.
UPDATE:
Shiny Code Tried:
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> — Map data © <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)
Getting an error saying object 'Precinct' not found.
