【发布时间】:2021-10-23 09:15:09
【问题描述】:
我是 Shiny 的新手。我正在尝试用竞争对手药房的积分和我的药房(Tim's pharmacies)的积分制作一个图层。我希望积分的颜色不同(比赛为红色,我的为绿色)。我想我需要从 leaflet() 中删除 pharmacy 并创建一个新的观察事件,但无法使其正常工作。有两个数据集:第一个是竞争对手药店,第二个是我的。我将它们与 rbind 结合起来,并认为我可以根据二进制编码的my_store 列指定颜色(我的为 1,竞争为 0)?任何帮助将不胜感激。
| ID | Label | Lat | Long | State |
|---|---|---|---|---|
| 1 | Bob's | 47.14032 | -107.334 | Montana |
| 2 | Bob's | 44.57247 | -116.125 | Montana |
| 3 | Evan's | 42.88031 | -111.989 | Idaho |
| 4 | Evan's | 42.93041 | -112.3654 | Idaho |
| 5 | Silvia's | 42.19124 | -112.7645 | Idaho |
| 6 | Evan's | 45.7939 | -108.768 | Montana |
| 7 | John's | 46.71677 | -106.752 | Wyoming |
| ID | Label | Lat | Long | State |
|---|---|---|---|---|
| 1 | Tim's | 47.22632 | -107.774 | Montana |
| 2 | Tim's | 44.67257 | -116.135 | Montana |
| 3 | Tim's | 42.88031 | -111.779 | Idaho |
| 4 | Tim's | 42.89041 | -112.3324 | Idaho |
| 5 | Tim's | 42.19124 | -112.7645 | Idaho |
| 6 | Tim's | 45.8539 | -108.658 | Montana |
| 7 | Tim's | 46.72887 | -106.7542 | Wyoming |
library(shiny)
library(leaflet)
library(dplyr)
library(leaflet.extras)
# install.packages("leaflet.extras")
pharmacy <- read_excel("~/pharmacy.xlsx")
My_Pharmacy <- read_excel("~/My_Pharmacy.xlsx")
all_stores <- rbind(pharmacy, My_Pharmacy)
all_stores <-
all_stores %>%
mutate(my_store = if_else(Label == "Tim's Pharmacy",1,0))
# Define UI
ui <- fluidPage(
# Application title
titlePanel("map"),
# Show a map output
mainPanel(
leafletOutput(outputId = "map_pharmacy"),
selectInput(inputId = "State",
label = "choose a store brand",
choices = unique(pharmacy$State))
)
)
# Define server logic required
server <- function(input, output, session) {
filteredData <- reactive({
pharmacy %>%
filter(State == input$State)
})
output$map_pharmacy <- renderLeaflet({
leaflet(pharmacy) %>% addTiles() %>%
fitBounds(~min(Long), ~min(Lat), ~max(Long), ~max(Lat))
})
observe({
leafletProxy("map_pharmacy", data = filteredData()) %>%
clearShapes() %>%
addCircles(color = "red", weight = 10)
})
}
# Run the application
shinyApp(ui = ui, server = server)```
【问题讨论】: