開始使用免費開始

大規模槍擊:顯示說明

為使用者提供更多關於你應用程式的背景資訊總是有幫助的。一種做法是 在應用程式中加入一個 About 按鈕,並以對話框(modal)顯示相關內容。

這正是你在本練習要完成的事。當使用者點擊「About」按鈕時, 你的應用程式會像螢幕截圖中那樣顯示。

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

回想一下,你可以使用以下程式碼片段,在對話框中顯示文字 'About':

showModal(modalDialog("About"))

本練習屬於課程

使用 R 的 Shiny 建立網頁應用程式

檢視課程

練習說明

  • 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)
編輯並執行程式碼