我想用R Shiny绘制一些数据点的空间分布图.当我将不同区域的过滤器添加到R脚本中时,它无法在默认 map 上显示所有区域的所有数据点.
对于如何改进 playbook 有什么建议?
下面是我的R脚本和一个示例数据:
library(shiny)
library(shinythemes)
library(leaflet)
# generate the data
dat <- data.frame(
region=sample(c("A","B","C"),100,replace=T),
x=rnorm(100, mean=50, sd=10),
y=rnorm(100, mean=40, sd=10)
)
# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
theme = shinytheme("united"),
# Application title
titlePanel("Test"),
# Sidebar
sidebarLayout(
sidebarPanel(
selectizeInput(
"regionInput", label=h3("Region"),choices=c("A","B","C"))
),
# Show a plot of the generated distribution
mainPanel(
leafletOutput(outputId = 'map')
)
)
)
)
# Define server logic to map the distribution
server <- shinyServer(function(input, output) {
output$map <- renderLeaflet({
leaflet(dat) %>%
addProviderTiles("Esri.WorldImagery") %>%
addCircleMarkers(color = "blue", radius = 2, stroke = FALSE, fillOpacity = 0.5, lng=dat$x[which(dat$region == input$regionInput)], lat=dat$y[which(dat$region == input$regionInput)])
})
})
# Run the application
shinyApp(ui = ui, server = server)