始める無料で始める

大量銃撃事件: ヘルプを表示する

アプリの背景情報をユーザーに示すことは常に有用です。その一つの方法は、アプリに About ボタンを追加し、モーダルダイアログで説明を表示することです。

この演習ではまさにそれを行います。ユーザーが "About" ボタンをクリックすると、次のスクリーンショットのような見た目になります。

An app displaying red circles for each mass shooting incident with details appearing on clicking the circle

次のスニペットで、モーダルダイアログに 'About' というテキストを表示できることを思い出してください:

showModal(modalDialog("About"))

この演習はコースの一部です

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

コースを見る

演習の手順

  • UI: show_about という名前のアクションボタンを追加します。
  • Server: observeEvent を使って、showModal(modalDialog(text_about, title = 'About')) によりモーダルダイアログを表示するイベントをトリガーします。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

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"
    ),
    # CODE BELOW: Add an action button named show_about
    
  ),
  tags$style(type = "text/css", "
    html, body {width:100%;height:100%}     
    #controls{background-color:white;padding:20px;}
  ")
)

server <- function(input, output, session) {
  # CODE BELOW: Use observeEvent to display a modal dialog
  # with the help text stored in text_about.

  
  
  output$map <- leaflet::renderLeaflet({
    mass_shootings %>% 
      filter(
        date >= input$date_range[1],
        date <= input$date_range[2],
        fatalities >= input$nb_fatalities
      ) %>% 
      leaflet() %>% 
      setView( -98.58, 39.82, zoom = 5) %>% 
      addTiles() %>% 
      addCircleMarkers(
        popup = ~ summary, radius = ~ sqrt(fatalities)*3,
        fillColor = 'red', color = 'red', weight = 1
      )
  })
}

shinyApp(ui, server)
コードを編集して実行