I am trying to create an interactive map that displays locations only in the district chosen by the user in R shiny.
rm(list=ls())
library(readxl)
library(shiny)
library(tidyverse)
library(leaflet)
setwd("C:/Users/Dulguun Sukhbat/Desktop/intern")
data <- read_xlsx("data.xlsx",sheet = 1)
data <- data %>% select(-c(total.price,location,"location:"))
data <- na.omit(data)
attach(data)
data$LAT <- as.numeric(data$LAT)
data$LONG <- as.numeric(data$LONG)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("district","Select a district:",
choices = unique(data$district))
),
mainPanel(
tabsetPanel(
tabPanel("Map")
)
)
)
)
#server <- function(input, output) {
# dist_choice <- reactive({
#d <- data %>% filter(district == input$district)
# return(d)
# })
#dist_choice <- dist_choice %>% mutate(popup_info=paste("built - ",built,"<br/>","area - ",
# square,"<br/>","sq.price - ",sq.price))
# }
server <- function(input, output) {
dist_choice <- reactive({
data %>%
filter(district %in% input$district) %>%
mutate(popup_info = paste("built - ",built,"<br/>","area - ",
square,"<br/>","sq.price - ",sq.price))
})
output$Map <- renderLeaflet({
leaflet(dist_choice()) %>% addTiles() %>% addCircleMarkers(data = dist_choice(),
lat = ~LAT,lng = ~LONG,radius=1,popup = ~popup_info)
})
}
shinyApp(ui = ui, server = server)
There is no error message when I run the app, but the map doesn't show up. Please help.
CodePudding user response:
You didn't include minimal reproducible example as data you use is not available for us (you can check function dput for this). But it looks that you just missed output function in UI.
Here:
mainPanel(
tabsetPanel(
tabPanel("Map")
)
)
You create mainPanel, then tabsetPanel as a place where tabPanels will live. And then tabPanel, but it is just a tab, you still need to tell that there you want to have your map. You can also run ?tabPanel to see examples in documentation.
It should be:
mainPanel(
tabsetPanel(
tabPanel("Map",
leafletOutput("Map")
)
)
)
