R Shiny,Leaflet 问题 - > SelectInput 以更改下拉菜单中的选择
Issue with R Shiny, Leaflet --> SelectInput to Change Selection in Drop Down Menu
我是 R Shiny 的新用户,我正在尝试绘制出可以从始发机场飞往的所有目的地。
当我将某个国家/地区硬编码到我的代码中时,我已经成功创建了一张显示该国家/地区所有机场的地图(我以意大利为例)。
我想要做的是有一个 "selectinput" 允许用户 select 一个国家并且所有相应的机场都会显示在地图上。
这是我的代码:
#----------Loading my data----------#
#Dataset 1: Routes
routes=read.csv(url("https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat"))
#Dataset #2: Airports
airports=read.csv(url("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports-extended.dat"))
#Give Better Names to Columns
colnames(routes)=c("Airline","AirlineID","IATA","SourceAP_ID","DestinationAirport","DestAP_ID","Codeshare","Stops","Equipment")
colnames(airports)=c("AirportID","Name","City","Country","IATA","ICAO","Latitude","Longitude","Altitude","Timezone","DST","TzDatabaseTz","Type","Source")
#Join datasets on Source Airport
fullair=merge(x=routes,y=airports,by="IATA",all.x=TRUE)
#----------Preprocessing Data---------#
fullair2=subset(fullair,fullair$Type=="airport")
fullair2$UniqueID=paste0(fullair2$IATA,"_",fullair2$DestinationAirport)
library(dplyr)
group_by(IATA) %>%
mutate(Count=n_distinct(UniqueID)) %>%
ungroup()
fullair3=as.data.frame(fullair3)
fullair3=fullair3[!duplicated(fullair3[c("UniqueID")]),]
library(rowr)
library(sqldf)
library(RSQLite)
library(stringi)
fullair3$Region=stri_extract(fullair3$TzDatabaseTz, regex='[^/]*')
SpitOutNum=sqldf("select IATA,count(*)
from fullair3
group by IATA")
SpitOutNum=as.data.frame(SpitOutNum3)
colnames(SpitOutNum)=c("IATA","DestinationCount")
fullair3=merge(x=fullair3,y=SpitOutNum,by="IATA",all.x=TRUE)
#Create the full name
fullair3$NamePart1=paste("(",fullair3$IATA,")",sep ="")
fullair3$FullName=paste(fullair3$Name, fullair3$NamePart1)
fullair3$NamePart1=NULL
#Make destination specific columns like long and lat
SpitOutNum2=sqldf("select IATA, City, Country, Region, Name, DestinationCount, Longitude,
Latitude
from fullair3
group by IATA,City, Country, Region, Name")
colnames(SpitOutNum2)=c("DestinationAirport","DestCity","DestCountry","DestRegion","DestAirportName","DestCount","DestLong","DestLat")
fullair3=merge(x=fullair3,y=SpitOutNum2,by="DestinationAirport",all.x=TRUE)
#--------------------R Shiny App-------------------#
library(shinydashboard)
library(shiny)
library(leaflet)
library(leaflet.extras)
library(rgdal)
library(sp)
library(raster)
airportchoices=unique(fullair3$FullName)
countrychoices=unique(fullair3$Country)
regionchoices=unique(fullair3$Region)
Italy=subset(fullair3,fullair3$Country=="Italy")
# Define UI for application
ui <- fluidPage(
dashboardPage(
dashboardHeader(title="Airport Data"),
dashboardSidebar(
sidebarMenu(
menuItem(
"Maps",
tabName = "maps",
icon=icon("globe")
)
)
),
dashboardBody(
tabItems(
tabItem(
tabName = "maps",
tags$style(type="text/css","#all_airports {height:calc(100vh - 80px) !important;}"),
leafletOutput("all_airports"),
selectInput(inputId = "countryselect",label="Select a country:",choices=countrychoices)
)
)
)
)
)
# Define server logic
server <- function(input, output) {
AirportData=reactive({
filteredData=subset(fullair3,Country == input$countryselect)
return(filteredData)
})
output$all_airports=renderLeaflet({
data=AirportData()
pal=colorNumeric("Reds",Italy$DestinationCount)
leaflet(data=Italy) %>%
addTiles(group="OpenStreetMap") %>%
addCircles(radius = ~Italy$DestinationCount*250,
weight = 1,
color = "black",
fillColor = ~pal(Italy$DestinationCount),
fillOpacity = 0.7,
popup = paste0("Airport Name: ", Italy$Name, "<br>",
"City: ", Italy$City, "<br>",
"Destination Count: ",Italy$DestinationCount,"<br>"
),
label = ~as.character(Italy$IATA),
group = "Points") #%>%
#addMarkers(lng = ~Longitude,lat = ~Latitude,
# popup=~as.character(DestinationCount),
# label=~as.character(DestinationCount),
# group = "Markers")
})
}
# Run the application
shinyApp(ui = ui, server = server)
这是我的问题:
我不确定如何让 select 输入下拉菜单显示在我的地图上,然后将其 selection 连接到地图。
我要如何更改上面的代码才能做到这一点?
如有任何帮助,我们将不胜感激!
您已经完成了所有必要的工作。只需将所有硬编码的意大利替换为数据,这将包含用户选择的国家/地区的机场数据子集。我还考虑将 selectInput
移动到仪表板的顶部,因为很难从底部滚动并且用户可能看不到它。我将它放在顶部中心以避免下拉选项被放大控件覆盖。
更新代码:
#----------Loading my data----------#
#Dataset 1: Routes
routes=read.csv(url("https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat"))
#Dataset #2: Airports
airports=read.csv(url("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports-extended.dat"))
#Give Better Names to Columns
colnames(routes)=c("Airline","AirlineID","IATA","SourceAP_ID","DestinationAirport","DestAP_ID","Codeshare","Stops","Equipment")
colnames(airports)=c("AirportID","Name","City","Country","IATA","ICAO","Latitude","Longitude","Altitude","Timezone","DST","TzDatabaseTz","Type","Source")
#Join datasets on Source Airport
fullair=merge(x=routes,y=airports,by="IATA",all.x=TRUE)
#----------Preprocessing Data---------#
fullair2=subset(fullair,fullair$Type=="airport")
fullair2$UniqueID=paste0(fullair2$IATA,"_",fullair2$DestinationAirport)
library(dplyr)
fullair3 = fullair2 %>%
group_by(IATA) %>%
mutate(Count=n_distinct(UniqueID)) %>%
ungroup()
fullair3=as.data.frame(fullair3)
fullair3=fullair3[!duplicated(fullair3[c("UniqueID")]),]
library(rowr)
library(sqldf)
library(RSQLite)
library(stringi)
fullair3$Region=stri_extract(fullair3$TzDatabaseTz, regex='[^/]*')
SpitOutNum=sqldf("select IATA,count(*)
from fullair3
group by IATA")
# SpitOutNum=as.data.frame(SpitOutNum3)
colnames(SpitOutNum)=c("IATA","DestinationCount")
fullair3=merge(x=fullair3,y=SpitOutNum,by="IATA",all.x=TRUE)
#Create the full name
fullair3$NamePart1=paste("(",fullair3$IATA,")",sep ="")
fullair3$FullName=paste(fullair3$Name, fullair3$NamePart1)
fullair3$NamePart1=NULL
#Make destination specific columns like long and lat
SpitOutNum2=sqldf("select IATA, City, Country, Region, Name, DestinationCount, Longitude,
Latitude
from fullair3
group by IATA,City, Country, Region, Name")
colnames(SpitOutNum2)=c("DestinationAirport","DestCity","DestCountry","DestRegion","DestAirportName","DestCount","DestLong","DestLat")
fullair3=merge(x=fullair3,y=SpitOutNum2,by="DestinationAirport",all.x=TRUE)
#--------------------R Shiny App-------------------#
library(shinydashboard)
library(shiny)
library(leaflet)
library(leaflet.extras)
library(rgdal)
library(sp)
library(raster)
airportchoices=unique(fullair3$FullName)
countrychoices=unique(fullair3$Country)
regionchoices=unique(fullair3$Region)
Italy=subset(fullair3,fullair3$Country=="Italy")
countrychoices <- as.character(countrychoices)
countrychoices <- sort(countrychoices)
# Define UI for application
ui <- fluidPage(
dashboardPage(
dashboardHeader(title="Airport Data"),
dashboardSidebar(
sidebarMenu(
menuItem(
"Maps",
tabName = "maps",
icon=icon("globe")
)
)
),
dashboardBody(
tabItems(
tabItem(
tabName = "maps",
tags$style(type="text/css","#all_airports {height:calc(100vh - 80px) !important;}"),
fluidRow(column(4),
column(8,
selectInput(inputId = "countryselect",label="Select a country:",choices=countrychoices, selected = "France")
)),
leafletOutput("all_airports")
)
)
)
)
)
# Define server logic
server <- function(input, output) {
AirportData=reactive({
filteredData=subset(fullair3,Country == input$countryselect)
return(filteredData)
})
output$all_airports=renderLeaflet({
data=AirportData()
pal=colorNumeric("Reds",data$DestinationCount)
leaflet(data=data) %>%
addTiles(group="OpenStreetMap") %>%
addCircles(radius = ~data$DestinationCount*250,
weight = 1,
color = "black",
fillColor = ~pal(data$DestinationCount),
fillOpacity = 0.7,
popup = paste0("Airport Name: ", data$Name, "<br>",
"City: ", data$City, "<br>",
"Destination Count: ",data$DestinationCount,"<br>"
),
label = ~as.character(data$IATA),
group = "Points") #%>%
#addMarkers(lng = ~Longitude,lat = ~Latitude,
# popup=~as.character(DestinationCount),
# label=~as.character(DestinationCount),
# group = "Markers")
})
}
# Run the application
shinyApp(ui = ui, server = server)
我是 R Shiny 的新用户,我正在尝试绘制出可以从始发机场飞往的所有目的地。
当我将某个国家/地区硬编码到我的代码中时,我已经成功创建了一张显示该国家/地区所有机场的地图(我以意大利为例)。
我想要做的是有一个 "selectinput" 允许用户 select 一个国家并且所有相应的机场都会显示在地图上。
这是我的代码:
#----------Loading my data----------#
#Dataset 1: Routes
routes=read.csv(url("https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat"))
#Dataset #2: Airports
airports=read.csv(url("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports-extended.dat"))
#Give Better Names to Columns
colnames(routes)=c("Airline","AirlineID","IATA","SourceAP_ID","DestinationAirport","DestAP_ID","Codeshare","Stops","Equipment")
colnames(airports)=c("AirportID","Name","City","Country","IATA","ICAO","Latitude","Longitude","Altitude","Timezone","DST","TzDatabaseTz","Type","Source")
#Join datasets on Source Airport
fullair=merge(x=routes,y=airports,by="IATA",all.x=TRUE)
#----------Preprocessing Data---------#
fullair2=subset(fullair,fullair$Type=="airport")
fullair2$UniqueID=paste0(fullair2$IATA,"_",fullair2$DestinationAirport)
library(dplyr)
group_by(IATA) %>%
mutate(Count=n_distinct(UniqueID)) %>%
ungroup()
fullair3=as.data.frame(fullair3)
fullair3=fullair3[!duplicated(fullair3[c("UniqueID")]),]
library(rowr)
library(sqldf)
library(RSQLite)
library(stringi)
fullair3$Region=stri_extract(fullair3$TzDatabaseTz, regex='[^/]*')
SpitOutNum=sqldf("select IATA,count(*)
from fullair3
group by IATA")
SpitOutNum=as.data.frame(SpitOutNum3)
colnames(SpitOutNum)=c("IATA","DestinationCount")
fullair3=merge(x=fullair3,y=SpitOutNum,by="IATA",all.x=TRUE)
#Create the full name
fullair3$NamePart1=paste("(",fullair3$IATA,")",sep ="")
fullair3$FullName=paste(fullair3$Name, fullair3$NamePart1)
fullair3$NamePart1=NULL
#Make destination specific columns like long and lat
SpitOutNum2=sqldf("select IATA, City, Country, Region, Name, DestinationCount, Longitude,
Latitude
from fullair3
group by IATA,City, Country, Region, Name")
colnames(SpitOutNum2)=c("DestinationAirport","DestCity","DestCountry","DestRegion","DestAirportName","DestCount","DestLong","DestLat")
fullair3=merge(x=fullair3,y=SpitOutNum2,by="DestinationAirport",all.x=TRUE)
#--------------------R Shiny App-------------------#
library(shinydashboard)
library(shiny)
library(leaflet)
library(leaflet.extras)
library(rgdal)
library(sp)
library(raster)
airportchoices=unique(fullair3$FullName)
countrychoices=unique(fullair3$Country)
regionchoices=unique(fullair3$Region)
Italy=subset(fullair3,fullair3$Country=="Italy")
# Define UI for application
ui <- fluidPage(
dashboardPage(
dashboardHeader(title="Airport Data"),
dashboardSidebar(
sidebarMenu(
menuItem(
"Maps",
tabName = "maps",
icon=icon("globe")
)
)
),
dashboardBody(
tabItems(
tabItem(
tabName = "maps",
tags$style(type="text/css","#all_airports {height:calc(100vh - 80px) !important;}"),
leafletOutput("all_airports"),
selectInput(inputId = "countryselect",label="Select a country:",choices=countrychoices)
)
)
)
)
)
# Define server logic
server <- function(input, output) {
AirportData=reactive({
filteredData=subset(fullair3,Country == input$countryselect)
return(filteredData)
})
output$all_airports=renderLeaflet({
data=AirportData()
pal=colorNumeric("Reds",Italy$DestinationCount)
leaflet(data=Italy) %>%
addTiles(group="OpenStreetMap") %>%
addCircles(radius = ~Italy$DestinationCount*250,
weight = 1,
color = "black",
fillColor = ~pal(Italy$DestinationCount),
fillOpacity = 0.7,
popup = paste0("Airport Name: ", Italy$Name, "<br>",
"City: ", Italy$City, "<br>",
"Destination Count: ",Italy$DestinationCount,"<br>"
),
label = ~as.character(Italy$IATA),
group = "Points") #%>%
#addMarkers(lng = ~Longitude,lat = ~Latitude,
# popup=~as.character(DestinationCount),
# label=~as.character(DestinationCount),
# group = "Markers")
})
}
# Run the application
shinyApp(ui = ui, server = server)
这是我的问题:
我不确定如何让 select 输入下拉菜单显示在我的地图上,然后将其 selection 连接到地图。
我要如何更改上面的代码才能做到这一点?
如有任何帮助,我们将不胜感激!
您已经完成了所有必要的工作。只需将所有硬编码的意大利替换为数据,这将包含用户选择的国家/地区的机场数据子集。我还考虑将 selectInput
移动到仪表板的顶部,因为很难从底部滚动并且用户可能看不到它。我将它放在顶部中心以避免下拉选项被放大控件覆盖。
更新代码:
#----------Loading my data----------#
#Dataset 1: Routes
routes=read.csv(url("https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat"))
#Dataset #2: Airports
airports=read.csv(url("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports-extended.dat"))
#Give Better Names to Columns
colnames(routes)=c("Airline","AirlineID","IATA","SourceAP_ID","DestinationAirport","DestAP_ID","Codeshare","Stops","Equipment")
colnames(airports)=c("AirportID","Name","City","Country","IATA","ICAO","Latitude","Longitude","Altitude","Timezone","DST","TzDatabaseTz","Type","Source")
#Join datasets on Source Airport
fullair=merge(x=routes,y=airports,by="IATA",all.x=TRUE)
#----------Preprocessing Data---------#
fullair2=subset(fullair,fullair$Type=="airport")
fullair2$UniqueID=paste0(fullair2$IATA,"_",fullair2$DestinationAirport)
library(dplyr)
fullair3 = fullair2 %>%
group_by(IATA) %>%
mutate(Count=n_distinct(UniqueID)) %>%
ungroup()
fullair3=as.data.frame(fullair3)
fullair3=fullair3[!duplicated(fullair3[c("UniqueID")]),]
library(rowr)
library(sqldf)
library(RSQLite)
library(stringi)
fullair3$Region=stri_extract(fullair3$TzDatabaseTz, regex='[^/]*')
SpitOutNum=sqldf("select IATA,count(*)
from fullair3
group by IATA")
# SpitOutNum=as.data.frame(SpitOutNum3)
colnames(SpitOutNum)=c("IATA","DestinationCount")
fullair3=merge(x=fullair3,y=SpitOutNum,by="IATA",all.x=TRUE)
#Create the full name
fullair3$NamePart1=paste("(",fullair3$IATA,")",sep ="")
fullair3$FullName=paste(fullair3$Name, fullair3$NamePart1)
fullair3$NamePart1=NULL
#Make destination specific columns like long and lat
SpitOutNum2=sqldf("select IATA, City, Country, Region, Name, DestinationCount, Longitude,
Latitude
from fullair3
group by IATA,City, Country, Region, Name")
colnames(SpitOutNum2)=c("DestinationAirport","DestCity","DestCountry","DestRegion","DestAirportName","DestCount","DestLong","DestLat")
fullair3=merge(x=fullair3,y=SpitOutNum2,by="DestinationAirport",all.x=TRUE)
#--------------------R Shiny App-------------------#
library(shinydashboard)
library(shiny)
library(leaflet)
library(leaflet.extras)
library(rgdal)
library(sp)
library(raster)
airportchoices=unique(fullair3$FullName)
countrychoices=unique(fullair3$Country)
regionchoices=unique(fullair3$Region)
Italy=subset(fullair3,fullair3$Country=="Italy")
countrychoices <- as.character(countrychoices)
countrychoices <- sort(countrychoices)
# Define UI for application
ui <- fluidPage(
dashboardPage(
dashboardHeader(title="Airport Data"),
dashboardSidebar(
sidebarMenu(
menuItem(
"Maps",
tabName = "maps",
icon=icon("globe")
)
)
),
dashboardBody(
tabItems(
tabItem(
tabName = "maps",
tags$style(type="text/css","#all_airports {height:calc(100vh - 80px) !important;}"),
fluidRow(column(4),
column(8,
selectInput(inputId = "countryselect",label="Select a country:",choices=countrychoices, selected = "France")
)),
leafletOutput("all_airports")
)
)
)
)
)
# Define server logic
server <- function(input, output) {
AirportData=reactive({
filteredData=subset(fullair3,Country == input$countryselect)
return(filteredData)
})
output$all_airports=renderLeaflet({
data=AirportData()
pal=colorNumeric("Reds",data$DestinationCount)
leaflet(data=data) %>%
addTiles(group="OpenStreetMap") %>%
addCircles(radius = ~data$DestinationCount*250,
weight = 1,
color = "black",
fillColor = ~pal(data$DestinationCount),
fillOpacity = 0.7,
popup = paste0("Airport Name: ", data$Name, "<br>",
"City: ", data$City, "<br>",
"Destination Count: ",data$DestinationCount,"<br>"
),
label = ~as.character(data$IATA),
group = "Points") #%>%
#addMarkers(lng = ~Longitude,lat = ~Latitude,
# popup=~as.character(DestinationCount),
# label=~as.character(DestinationCount),
# group = "Markers")
})
}
# Run the application
shinyApp(ui = ui, server = server)