銃乱射事件

Rで作るShiny Webアプリケーション

Ramnath Vaidyanathan

VP of Product Research

データを確認

銃乱射データセットの先頭10行

Rで作るShiny Webアプリケーション

各銃乱射事件を示す赤い円と、クリックで詳細が表示されるアプリ

Rで作るShiny Webアプリケーション

UI を追加

ui <- bootstrapPage(
  theme = shinythemes::shinytheme('simplex'),

leaflet::leafletOutput('map', width = '100%', height = '100%'),
absolutePanel(top = 10, right = 10, id = 'controls',
sliderInput('nb_fatalities', 'Minimum Fatalities', 1, 40, 10),
dateRangeInput('date_range', 'Select Date', "2010-01-01", "2019-12-01"),
)
, tags$style(type = "text/css", " html, body {width:100%;height:100%} #controls{background-color:white;padding:20px;} ")
)
Rで作るShiny Webアプリケーション

出力を追加:インタラクティブ地図

server <- function(input, output, session){

output$map <- leaflet::renderLeaflet({
leaflet() %>%
addTiles() %>%
setView( -98.58, 39.82, zoom = 5)
})
}
Rで作るShiny Webアプリケーション

米国のインタラクティブ地図と、期間と死亡者数を選ぶコントロールを持つアプリ

Rで作るShiny Webアプリケーション

リアクティブ式を追加

rval_mass_shootings <- reactive({

mass_shootings %>%
filter(
date >= input$date_range[1],
date <= input$date_range[2],
fatalities >= input$nb_fatalities
)
})
Rで作るShiny Webアプリケーション

出力を更新:インタラクティブ地図

output$map <- leaflet::renderLeaflet({

rval_mass_shootings() %>%
leaflet() %>% addTiles() %>% setView( -98.58, 39.82, zoom = 5) %>%
addCircleMarkers(
popup = ~ summary,
radius = ~ fatalities,
fillColor = 'red', color = 'red', weight = 1
)
})
Rで作るShiny Webアプリケーション

各銃乱射事件を示す赤い円と、クリックで詳細が表示されるアプリ

Rで作るShiny Webアプリケーション

アプリ更新:アクションボタンとモーダルを追加

ui <- bootstrapPage(
  theme = shinythemes::shinytheme('simplex'),
  leaflet::leafletOutput('map', width = '100%', height = '100%'),
  absolutePanel(top = 10, right = 10, id = 'controls',
    sliderInput('nb_fatalities', 'Minimum Fatalities', 1, 40, 10),
    dateRangeInput('date_range', 'Select Date', "2010-01-01", "2019-12-01"),

actionButton('show_about', 'About')
) )
server <- function(input, output, session){

observeEvent(input$show_about, {
showModal(modalDialog(text_about, title = 'About'))
})
}
Rで作るShiny Webアプリケーション

各銃乱射事件を示す赤い円と、クリックで詳細が表示されるアプリ

Rで作るShiny Webアプリケーション

練習しましょう!

Rで作るShiny Webアプリケーション

Preparing Video For Download...