在 R Shiny 中集成传单地图 - 按国家和症状输入选择
integrating leaflet map in RShiny - inputselect by country and symptom
我正在尝试在 Shiny R 中使用传单创建地图。我有几个问题:
如何通过按国家/地区选择症状来创建地图?如您所见,数据包含行中的国家和症状(参见下面提供的 GitHub 上的 link)。如果我想按特定国家和特定症状进行过滤,我该如何使用 Shiny r 中的传单进行过滤?
我想创建一个可拖动的下拉菜单(可以在其中选择症状 - 请参阅问题 1),因为我无法在整个屏幕上调整地图。可拖动下拉菜单的示例,名为 'Zip Explored' 我已尝试复制,但没有成功 - https://shiny.rstudio.com/gallery/superzip-example.html
我无法让地图显示在整个屏幕上。有没有办法在整个屏幕上显示地图?就像网络中的例子-link第二点。
代码如下:
library(shiny)
library(cvindia)
library(tidyverse)
library(shinydashboard)
server = function(input, output, session){}
ui <- fluidPage(
# Application title
h1("Symptoms accross the world"),
# Sidebar with a slider input for number of bins
selectInput("productCategory", "Select Country", c( "Bangladesh", "India", "Nigeria", "Pakistan", "United Kingdom")),
selectInput("productCategory", "Symptom", c("Chills", "Cough", "Muscle Ache"))
)
server <- function(input, output) {
}
# Run the application
shinyApp(ui = ui, server = server)
如果上面的代码是 运行,那么我已经很容易地创建了按国家和症状的 selectInput。
这是我的第二个代码,我不明白它应该如何与服务器交互,考虑到我感兴趣的值,并假设它应该与用户界面交互在行中:
leaflet() %>%
addTiles()
map <- leaflet(gather_divided) %>% addTiles() %>% addMarkers(clusterOptions = markerClusterOptions())
map
数据集样本在我的 GitHub 上,因为我还没有找到将它部署到堆栈溢出的更优雅的方法:
https://github.com/gabrielburcea/Whosebug_fake_data/tree/master
这是一个简短的演示,希望对您有所帮助。
一些注意事项:
- 确保每个
selectInput
都有唯一的 inputId
。两者的 ID 相同。
- 您可能希望将
multiple = TRUE
添加到 selectInput
以允许选择多个国家或症状
- 您可以在
server
中使用 reactive
表达式根据输入选择过滤数据
这也可能有助于将 leaflet
与 shiny
结合使用:
https://rstudio.github.io/leaflet/shiny.html
如果这是您的想法,请告诉我。
library(shiny)
library(tidyverse)
library(leaflet)
fake_data <- read.csv("https://raw.githubusercontent.com/gabrielburcea/Whosebug_fake_data/master/gather_divided.csv")
ui <- fluidPage(
# Application title
h1("Symptoms accross the world"),
# Inputs for country and symptom
selectInput("country", "Select Country", c("Bangladesh", "India", "Nigeria", "Pakistan", "United Kingdom"), multiple = TRUE),
selectInput("symptom", "Symptom", c("Chills", "Cough", "Muscle Ache"), multiple = TRUE),
# Output with map
h2("Map"),
leafletOutput("map")
)
server <- function(input, output) {
filtered_data <- reactive({
fake_data %>%
filter(Country %in% input$country,
Symptom %in% input$symptom)
})
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addMarkers(data = filtered_data())
})
}
# Run the application
shinyApp(ui = ui, server = server)
编辑:
要使您的 selectInput
出现在浮动、可拖动的菜单中,您可以使用 absolutePanel
(如您引用的示例中所示)。
请注意,该示例使用了自定义 .css,这可以进一步改善闪亮应用的外观。
ui <- fluidPage(
h1("Symptoms accross the world"),
leafletOutput("map"),
# Floating panel
absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
width = 330, height = "auto",
h2("Data Explorer"),
# Inputs for country and symptom
selectInput("country", "Select Country", c("Bangladesh", "India", "Nigeria", "Pakistan", "United Kingdom"), multiple = TRUE),
selectInput("symptom", "Symptom", c("Chills", "Cough", "Muscle Ache"), multiple = TRUE)
)
)
我正在尝试在 Shiny R 中使用传单创建地图。我有几个问题:
如何通过按国家/地区选择症状来创建地图?如您所见,数据包含行中的国家和症状(参见下面提供的 GitHub 上的 link)。如果我想按特定国家和特定症状进行过滤,我该如何使用 Shiny r 中的传单进行过滤?
我想创建一个可拖动的下拉菜单(可以在其中选择症状 - 请参阅问题 1),因为我无法在整个屏幕上调整地图。可拖动下拉菜单的示例,名为 'Zip Explored' 我已尝试复制,但没有成功 - https://shiny.rstudio.com/gallery/superzip-example.html
我无法让地图显示在整个屏幕上。有没有办法在整个屏幕上显示地图?就像网络中的例子-link第二点。
代码如下:
library(shiny)
library(cvindia)
library(tidyverse)
library(shinydashboard)
server = function(input, output, session){}
ui <- fluidPage(
# Application title
h1("Symptoms accross the world"),
# Sidebar with a slider input for number of bins
selectInput("productCategory", "Select Country", c( "Bangladesh", "India", "Nigeria", "Pakistan", "United Kingdom")),
selectInput("productCategory", "Symptom", c("Chills", "Cough", "Muscle Ache"))
)
server <- function(input, output) {
}
# Run the application
shinyApp(ui = ui, server = server)
如果上面的代码是 运行,那么我已经很容易地创建了按国家和症状的 selectInput。
这是我的第二个代码,我不明白它应该如何与服务器交互,考虑到我感兴趣的值,并假设它应该与用户界面交互在行中:
leaflet() %>%
addTiles()
map <- leaflet(gather_divided) %>% addTiles() %>% addMarkers(clusterOptions = markerClusterOptions())
map
数据集样本在我的 GitHub 上,因为我还没有找到将它部署到堆栈溢出的更优雅的方法:
https://github.com/gabrielburcea/Whosebug_fake_data/tree/master
这是一个简短的演示,希望对您有所帮助。
一些注意事项:
- 确保每个
selectInput
都有唯一的inputId
。两者的 ID 相同。 - 您可能希望将
multiple = TRUE
添加到selectInput
以允许选择多个国家或症状 - 您可以在
server
中使用reactive
表达式根据输入选择过滤数据
这也可能有助于将 leaflet
与 shiny
结合使用:
https://rstudio.github.io/leaflet/shiny.html
如果这是您的想法,请告诉我。
library(shiny)
library(tidyverse)
library(leaflet)
fake_data <- read.csv("https://raw.githubusercontent.com/gabrielburcea/Whosebug_fake_data/master/gather_divided.csv")
ui <- fluidPage(
# Application title
h1("Symptoms accross the world"),
# Inputs for country and symptom
selectInput("country", "Select Country", c("Bangladesh", "India", "Nigeria", "Pakistan", "United Kingdom"), multiple = TRUE),
selectInput("symptom", "Symptom", c("Chills", "Cough", "Muscle Ache"), multiple = TRUE),
# Output with map
h2("Map"),
leafletOutput("map")
)
server <- function(input, output) {
filtered_data <- reactive({
fake_data %>%
filter(Country %in% input$country,
Symptom %in% input$symptom)
})
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addMarkers(data = filtered_data())
})
}
# Run the application
shinyApp(ui = ui, server = server)
编辑:
要使您的 selectInput
出现在浮动、可拖动的菜单中,您可以使用 absolutePanel
(如您引用的示例中所示)。
请注意,该示例使用了自定义 .css,这可以进一步改善闪亮应用的外观。
ui <- fluidPage(
h1("Symptoms accross the world"),
leafletOutput("map"),
# Floating panel
absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
width = 330, height = "auto",
h2("Data Explorer"),
# Inputs for country and symptom
selectInput("country", "Select Country", c("Bangladesh", "India", "Nigeria", "Pakistan", "United Kingdom"), multiple = TRUE),
selectInput("symptom", "Symptom", c("Chills", "Cough", "Muscle Ache"), multiple = TRUE)
)
)