library(shiny)
library(bslib)
library(leaflet)
library(dplyr)
library(DT)
# 模拟地震数据
set.seed(123)
<- data.frame(
earthquake_data Magnitude = round(runif(100, min = 4, max = 9),2),
Latitude = runif(100, min = -90, max = 90),
Longitude = runif(100, min = -180, max = 180),
Year = 3000:3099
)
# 定义 UI
<- page_navbar(
ui title = "地震数据可视化",
sidebar=sidebar(
selectInput("year", "年份:",
choices = c("全部", unique(earthquake_data$Year)),
selected = "全部")
),nav_panel("地图", leafletOutput("earthquake_map")),
nav_panel("数据", DTOutput("earthquake_table"))
)
# 定义 server
<- function(input, output, session) {
server # 过滤数据
<- reactive({
filtered_data if (input$year == "全部") {
return(earthquake_data)
else {
} return(filter(earthquake_data, Year == input$year))
}
})
# 输出地震数据表格 ©微信公众号:R数据分析与可视化
$earthquake_table <- renderDT({
outputdatatable(filtered_data(),
options = list(
paging = TRUE,
searching = TRUE,
lengthMenu = c(5, 10, 20),
pageLength = 10
)
)
})
# 输出地震地图
$earthquake_map <- renderLeaflet({
outputleaflet(filtered_data()) %>%
addTiles() %>%
addMarkers(~Longitude, ~Latitude, popup = ~paste("Magnitude:", Magnitude, "<br>Year:", Year))
})
}
# 运行应用
shinyApp(ui, server)
Shiny应用-地震数据可视化
R语言
Shiny开发
地震数据可视化,地图