开始使用免费开始使用

大规模枪击事件:显示帮助

为用户提供更多关于应用的背景信息总是有帮助的。一种方式是在应用中添加一个 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)
编辑并运行代码