我有两个数据框,如下所示。为了简单起见,我只放了部分数据:
**order**
Location Order_count Longitude Latitude
New York 12 74.0060 40.7128
San Francisco 7 122.4194 37.7749
Miami 13 80.1918 25.7617
Texas 20 99.9018 31.9686
Dallas 1 96.7970 32.7767
Mary 0 27.6648 81.5158
Whitney 9 32.3182 86.9023
**arrivals**
Cust_id Arrival_time Location
1 3 New York
2 18 Miami
3 20 Texas
4 1 New York
5 0 Dallas
6 18 Miami
7 20 Dallas
8 1 New York
首先,我使用订单数据框中的经度和纬度符号来映射传单上的位置。
然后我放置了一个选择到达时间的选择滑块。根据所选时间,该位置(地图上)的客户数量应该发生变化。该地图还显示了该特定位置所有时间段内的总到达人数。例如,如果我选择 Arrival_time=1,如果我在地图上单击纽约,它应该显示:
“纽约”,
“所选时间的顾客数量:2”,
“顾客总数:3”。
我用以下方式编码:
**ui**
shinyUI(fluidpage(
titlePanel("Time"),
sidebarLayout(
sidebarPanel(
sliderInput("time", "Hours selected ", min=0, max=24, value=12)
),
mainPanel()),
leafletOutput("mymap", height = "900", width = "900")))
**server**
shinyServer(function(input, output){
data_input <- reactive({arrivals%>%
filter(Arrival_time== input$time) %>%
group_by(Location) %>% count()
})
output$mymap <- renderLeaflet({
leaflet(data=order) %>% addTiles() %>%
addMarkers(~Longitude, ~Latitude, popup = paste(order$Location,
"Total arrivals for the selected time: ", arrivals$data_input,"
order$Order_count))
})})
地图不会随着滑块提供的输入而改变。我不知道我哪里做错了。
将
arrivals$data_input
替换为 data_input()$n
编辑:根据我们的评论,您最初的问题看起来更像是合并不同的数据集
library(shiny)
library(leaflet)
library(dplyr)
order <- data.frame(
Location = c('New York', 'San Francisco', 'Miami', 'Texas', 'Dallas', 'Mary', 'Whitney'),
Order_count = c(12, 7, 13, 20, 1, 0 ,9),
Longitude = c(-74.0060, -122.4194, -80.1918, -99.9018, -96.7970, -81.5158, -86.9023),
Latitude = c(40.7128, 37.7749, 25.7617, 31.9686, 32.7767, 27.6648, 32.3182)
)
arrivals = data.frame(
Cust_id = c(1,2,3,4,5,6,7,8),
Arrival_time = c(3,18,20,1,0,18,20,1),
Location = c('New York', 'Miami', 'Texas', 'New York', 'Dallas', 'Miami', 'Dallas', 'New York')
)
# Make a dataframe containing needed variables by merging data
join_data <- order %>%
inner_join(arrivals %>%
group_by(Location, Arrival_time) %>%
summarise(number_of_customer = n()),
by = "Location") %>%
left_join(arrivals %>%
group_by(Location) %>%
summarise(total_number_of_customer = n()),
by = "Location")
ui <- shinyUI(fixedPage(
titlePanel("Time"),
sidebarLayout(
sidebarPanel(
sliderInput("time", "Hours selected ", min=0, max=24, value=1)
),
mainPanel()),
leafletOutput("mymap", height = "900", width = "900")))
server <- shinyServer(function(input, output, session){
data_input <- reactive({
join_data %>%
filter(Arrival_time==input$time)
})
output$mymap <- renderLeaflet({
leaflet(data=order) %>%
addTiles() %>%
addMarkers(
~Longitude,
~Latitude,
popup = paste(
order$Location, "<br>",
"Number of customers for the selected time ", data_input()$number_of_customer,"
<br>","Total number of customers: ", data_input()$total_number_of_customer))
})})
shinyApp(ui, server)