简体   繁体   English

使用传单/光泽的R中的交互式地图

[英]Interactive Map in R using leaflet/Shiny

I want to create a map with a sidebar where you can select the range of a time period. 我想创建一个带有侧边栏的地图,您可以在其中选择时间段的范围。 The idea is that the map shows marks only for hotels which opened during the period. 想法是,地图仅显示该时期内开业的酒店的标记。 I had no clue to do that so I tried to use selectInput instead since it seemed a little easier for me. 我不知道这样做,所以我尝试使用selectInput,因为它对我来说似乎更容易一些。 But it seems I still can't make it work. 但是似乎我仍然无法使它工作。 Any guidance would be appreciated. 任何指导将不胜感激。

Here's the sample data frame: 这是示例数据框:

Hotel Year   lat        long
  A   2000 41.886337 -87.628472
  B   2005 41.88819  -87.635199
  C   2010 41.891113 -87.63301

Here's the ui.R: 这是ui.R:

#ui.R
library(shiny)
library(leaflet)

shinyUI(fluidPage(
  titlePanel("Hotel Map"),
  sidebarLayout(
    sidebarPanel(
      selectInput("year", 
                  label = "Choose Year:",
                  choices = c(2000,2005,2010),
                  selected = 2000
      )),

    mainPanel (leafletOutput("map","100%",300))
  )
))

Here's the server.R: 这是服务器。R:

library(shiny)
library(leaflet)
source("RStudio\\Map-app")
hotels <- read.csv("RStudio\\Map-app\\ChicagoHotels.csv")

shinyServer(
  function(input, output) {

    output$map <- renderLeaflet({
    df <- hotels[hotels$Year == input$year,]  
    leaflet()  %>%
        addTiles() %>%
        addCircles(data = df)
    })

  }
    )

@InfiniteFlashChess That´s correct. @InfiniteFlashChess是的。

Server.R Server.R

library(leaflet)

hotels <- read.table(text = "Hotel Year  latitude        longitude
                              A   2000  41.886337      -87.628472
                              B   2005  41.88819       -87.635199
                              C   2010  41.891113      -87.63301", 
                     header = TRUE)

shinyServer(function(input, output) {

output$map <- renderLeaflet({
  df <- hotels[hotels$Year == input$year,]  
  leaflet()  %>%
    addTiles() %>%
    addMarkers(data = df)
 })

})

Ui.R Ui.R

library(shiny)
library(leaflet)

shinyUI(fluidPage(
titlePanel("Hotel Map"),
 sidebarLayout(
  sidebarPanel(
   selectInput("year", 
               label = "Choose Year:",
               choices = c(2000,2005,2010),
               selected = 2000
  )),

mainPanel (leafletOutput("map","100%",300))

              )
))

Well first of all, something you should look to undersand is what the mainPanel is doing in the first place. 首先,您应该注意的是mainPanel首先要做的事情。 The mainPanel will NOT take in mainPanel将不接受

leafletMap("map","100%",300)

It requires you to input this instead: 它要求您输入以下内容:

leafletOutput("map", "100%", 300)

or some other related leafletOuput statement. 或其他一些相关的leafletOuput语句。

The way shiny is designed, if you want graphs or tables to output, you need to have the output$map object you've created in the server.R file, come back to the ui.R and your graph will only output based off a specifc output statement. 设计光泽的方式是,如果要输出图形或表格,则需要在server.R文件中创建已创建的output $ map对象,然后返回ui.R,图形将仅基于具体的输出语句。

This page has examples and help galore. 此页面包含示例和帮助。

Leaflet link you should really look at, so click me! 您应该真正查看的传单链接,所以请单击我!

Thank you for introducing me to leaflet by the way, this is a much better tool than the googleVis package's gvisGeoMap feature. 谢谢您向我介绍传单,它比googleVis软件包的gvisGeoMap功能要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM