在RShiny中集成传单地图-输入按国家和症状选择 [英] integrating leaflet map in RShiny - inputselect by country and symptom

查看:70
本文介绍了在RShiny中集成传单地图-输入按国家和症状选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在Shiny R中创建带有传单的地图.我有几个问题:

I am attempting to create a map within Shiny R, with leaflet. I have several questions:

  1. 如何通过选择国家症状"来创建地图?如您所见,数据按行包含国家和症状(请参见下面提供的GitHub上的链接).如果我想按某个国家和某些症状进行过滤,该如何使用Shiny r中的传单进行此操作?

  1. How to create the map by selecting Symptoms by Country? as you can see data contains countries and symptoms in rows (see link on GitHub provided bellow). if I want to filter by a certain country and certain symptom how do I do this with leaflet in Shiny r?

我想创建一个可拖动的下拉菜单(可以选择症状-请参阅问题1),因为我无法在整个屏幕上调整地图.我尝试复制的,名为"Zip Exploreed"的可拖动下拉菜单示例在这里没有成功- https://shiny.rstudio.com/gallery/superzip-example.html

I want created a draggable drop down menu (where symptoms can be chosen - see question 1) since I cannot adjust the map on the entire screen. An example of a draggable drop down menu, called 'Zip Explored' I have tried to replicate, with no success is here - https://shiny.rstudio.com/gallery/superzip-example.html

我无法使地图显示在整个屏幕上.有没有办法在整个屏幕上显示地图?就像第二点的Web链接中的示例一样.

I could not make the map to show on the entire screen. Is there a way to show the map on the entire screen? Just like the example in the web-link on 2nd point.

下面是代码:

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.

If the above code is run, then I have easily managed to create the selectInput by country and symptom.

这是我拥有的第二个代码,我不了解它应如何与服务器交互,请记住我感兴趣的值,并假定它应与用户界面交互位于行中:

Here is the second code I have which I do not understand how it should interact with the server, having in mind the values I am interested, and presume it should interact with user interface are in the rows:

leaflet() %>%
  addTiles()

map <- leaflet(gather_divided) %>% addTiles() %>% addMarkers(clusterOptions = markerClusterOptions())

map

数据集的示例位于我的GitHub上,因为我还没有找到一种更优雅的方法来将其部署到堆栈溢出:

Sample of a dataset is on my GitHub as I haven't found a more elegant way of deploying it to stack overflow:

https://github.com/gabrielburcea/stackoverflow_fake_data/tree/master

推荐答案

下面是一个简短的演示,希望对您有所帮助.

Here is a brief demo that I hope will be helpful.

一些注意事项:

  • 确保每个 selectInput 都有唯一的 inputId .两者的ID相同.
  • 您可能想在 selectInput 中添加 multiple = TRUE ,以允许选择多个国家或地区症状
  • 您可以在 server 中使用 reactive 表达式根据输入选择来过滤数据
  • Make sure each selectInput has a unique inputId. You have the same id for both.
  • You may want to add multiple = TRUE to selectInput to allow for more than one country or symptom to be selected
  • You can use a reactive expression in server to filter the data based on the input selections

这对于将 leaflet shiny 一起使用也可能是有用的参考:

This also might be helpful reference for using leaflet with shiny:

https://rstudio.github.io/leaflet/shiny.html

让我知道这是否是您的初衷.

Let me know if this is what you had in mind.

library(shiny)
library(tidyverse)
library(leaflet)

fake_data <- read.csv("https://raw.githubusercontent.com/gabrielburcea/stackoverflow_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 (如您所引用的示例).

To make your selectInput appear in a floating, draggable menu, you can use absolutePanel (as in the example you referenced).

请注意,该示例使用了自定义.css,可以进一步改善闪亮应用的外观.

Note that the example uses custom .css which can improve the appearance of your shiny app further.

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)
  )
)

这篇关于在RShiny中集成传单地图-输入按国家和症状选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆