在 Shiny 中组合日期滑块、单选按钮和地图

Combine date slider, radio button, and map in Shiny

我想创建一个按日期和位置显示交通 KPI 的地图。用户可以使用滑块 select 一天的流量,以及使用单选按钮的流量 KPI。数据未显示在地图上。

我创建了一个反应对象,它根据单选按钮和滑块过滤数据。呈现 LeafLet 地图的代码在应用程序外部工作,显示数据的圆圈。

数据框结构如下:

date,lat,long,pageviews,unique_visitors
01.01.2019,6.7304,-3.49,206,238
04.01.2019,7.1604,3.35,223,275
07.01.2019,52.25,-4.25,272,407
10.01.2019,46.9757,-123.8095,44,448
13.01.2019,45.4646,-98.468,98,269
16.01.2019,35.1351,-79.432,443,337
19.01.2019,39.5146,-76.173,385,21
22.01.2019,57.1704,-2.08,273,371
25.01.2019,18.2301,42.5001,115,195
28.01.2019,5.32,-4.04,7,27
31.01.2019,32.4543,-99.7384,217,136
03.02.2019,38.923,-97.2251,337,15
06.02.2019,2.7017,33.6761,201,390
09.02.2019,36.7089,-81.9713,177,201
12.02.2019,30.1204,74.29,65,82
15.02.2019,5.4667,-3.2,261,229
18.02.2019,7.1904,1.99,364,38
21.02.2019,3.9837,13.1833,131,74
24.02.2019,-22.7167,-65.7,357,198
27.02.2019,39.4228,-74.4944,297,399
02.03.2019,24.4667,54.3666,382,147
05.03.2019,34.4504,40.9186,8,373
08.03.2019,9.0833,7.5333,83,182
11.03.2019,-9.6954,-65.3597,243,444
14.03.2019,16.85,-99.916,420,29

-> 它存储在应用程序外部的 "joined" 下(我正在加入两个表),我在反应对象中的管道开头调用它

当我 select 日期和指标时,输出结构如下:

经纬度selected_metric

lat是纬度,long是经度

我想问题是我如何在 renderLeaflet 中调用数据框,因为它是一个反应对象我不确定 ~ 命令是否可以调用列。

# Required packages
library(shiny)
library(leaflet)
library(dplyr)

# Define UI for application that shows a map
ui <- fluidPage(

    # App title
    titlePanel("Metrics by location"),

    # Input: select date range
    sliderInput("traffic_date",
                "Date:",
                min = as.Date("2019-01-01","%Y-%m-%d"),
                max = as.Date("2019-07-31","%Y-%m-%d"),
                value=as.Date("2019-07-31"),
                timeFormat="%Y-%m-%d"),

    # Input: select metric        
    radioButtons("metric",
                 "Metric",
                 c("Pageviews" = "pageviews", 
                   "Unique Visitors" = "unique_visitors"),
                 selected = "pageviews"),

    # Main panel for Output
    mainPanel(
        # Output: map
        leafletOutput("mymap")
    )
)

# Define server commands to draw map with data
server <- function(input, output) {

    # Reactive expression to generate dataframe for selected date and metric
    d <- reactive({

        day <- input$traffic_date
        show_metric <- input$metric

        d <- joined %>% 
            filter(date == day) %>% 
            select(lat,long,show_metric) %>%
            rename(selected_metric = show_metric)

        })
    # Note: the last pipeline element renames the metric column back to a neutral name

    #create the map
    output$mymap <- renderLeaflet({
        leaflet(d()) %>% 
            addTiles() %>% 
            setView(8.36,46.84,7) %>%
            addCircles(lat = ~ lat, 
                       lng = ~ long, 
                       weight = 1, 
                       radius = ~ selected_metric)
    })
}

# Run app
shinyApp(ui, server)

目前代码 returns 是一张空地图,我不确定我缺少哪一步来显示圆圈。

感谢您的帮助!

我认为您的问题是半径的使用。见下文摘自 help documentation:

半径
圆的半径数值向量;它也可以是一个单向公式,在这种情况下,半径值是从数据中得出的(圆的单位为米,圆标记的单位为像素)

我意识到那里的标记非常小。尝试将 selected_metric 乘以 10000 或更改为使用 addCircleMarkers.

更新

使用您的数据集,我将其转换为适用的日期和数字并删除 setView(),以便地图自动缩放到该范围之外的点。我遇到的问题之一是我最初看不到点,例如在非洲。上面范围内的许多日期也没有显示圆圈的数据。

# Required packages
library(shiny)
library(leaflet)
library(dplyr)

# Define UI for application that shows a map
ui <- fluidPage(

    # App title
    titlePanel("Metrics by location"),

    # Input: select date range
    sliderInput("traffic_date",
                "Date:",
                min = as.Date("2019-01-01","%Y-%m-%d"),
                max = as.Date("2019-07-31","%Y-%m-%d"),
                value=as.Date("2019-01-01"),
                timeFormat="%Y-%m-%d"),

    # Input: select metric        
    radioButtons("metric",
                 "Metric",
                 c("Pageviews" = "pageviews", 
                   "Unique Visitors" = "unique_visitors"),
                 selected = "pageviews"),

    # Main panel for Output
    mainPanel(
        # Output: map
        leafletOutput("mymap")
    )
)

# Define server commands to draw map with data
server <- function(input, output) {

    # Reactive expression to generate dataframe for selected date and metric
    d <- reactive({

        day <- input$traffic_date
        show_metric <- input$metric

        d <- joined %>% 
            filter(date == day) %>% 
            select(lat,long,show_metric) %>%
            rename(selected_metric = show_metric)

        })
    # Note: the last pipeline element renames the metric column back to a neutral name

    #create the map
    output$mymap <- renderLeaflet({
        leaflet(d()) %>% 
            addTiles() %>% 
            # setView(8.36,46.84,7) %>%
            addCircles(lat = ~ lat, 
                       lng = ~ long, 
                       weight = 1, 
                       radius = ~ selected_metric)
    })
}

# Run app
shinyApp(ui, server)