如何美化闪亮的传单地图?

时间:2019-05-08 18:31:00

标签: r shiny leaflet react-leaflet shiny-reactivity

我终于**获得了可以使用的下拉菜单,但还有一些挥之不去的问题:

  • 我的传单地图会自动在下拉菜单的“所有”选项上打开,但不会显示所有的圆圈标记(但是,在下拉菜单中选择艺术家姓名时,它会显示标记)–如何打开地图时显示所有圆圈标记(例如本例:https://samveverka.shinyapps.io/shinyapp/

  • 在我的UI中,当我添加更多的selectInputs(下拉列表)时,它们不会出现在地图上(这就像它们没有注册为输入-我不知道为什么吗!!)

  • 是否可以添加一些信息,例如指向我的github存储库的链接,数据源等?

欢迎其他任何改进建议。预先感谢!

##################
GLOBAL
##################

library(shiny)
library(shinythemes)
library(leaflet)
library(leaflet.extras)
library(RColorBrewer)
library(formattable)
library(dplyr)
library(stringr)

    ## load data ##
    murals <- read.csv("https://data.cityofchicago.org/api/views/we8h-apcf/rows.csv?accessType=DOWNLOAD",
                   stringsAsFactors = F, na = c("", "N/A", "NA"))

    ## clean data ##

      # clean Media type
      murals$Media <- str_replace(murals$Media, "spray", "Spray")
      murals$Media <- str_replace(murals$Media, "Spray + brush", "Spray and Brush")
      murals$Media <- str_replace(murals$Media, "Painting", "Paint")
      murals$Media <- str_replace(murals$Media, "LAKE VIEW", "Lake View")

      # clean Titles 
      murals$Artwork.Title <- str_replace(murals$Artwork.Title, "Where There Is Discord, Harmony:The Power of Art", "Where There Is Discord, Harmony: The Power of Art")


    ## make map easier to read with scalable circle markers ##
    circle.scaler <- function(x){((x-min(x))/(max(x)-min(x)))*500}

##################
UI 
##################

# set UI
ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("mymap", width = "100%", height = "100%"),
  absolutePanel(top = 10, right = 10,

                theme = shinytheme("lumen"),
                shinyjs::inlineCSS(list(body = "color:White")),
                titlePanel("Chicago Neighborhood Murals"),

                selectInput("Artist.Credit", 
                            label = "Artist",
                            choices = c("All",
                                        unique(as.character(murals$Artist.Credit))))))


##################
SERVER
################## 

# set server 
server <- function(input,output, session){

    filtered <- reactive({
        murals[murals$Artist.Credit == input$Artist.Credit, ] 
    })


    # define map color markers  
    color <- colorFactor(topo.colors(3), murals$Affiliated..or.Commissioning..Organization)

    # render original leaflet map 
    output$mymap <- renderLeaflet({
        leaflet(data = murals) %>%
            addTiles() %>%
            addMarkers() %>%

    # add legend     
        addLegend(
            "bottomleft", # legend position
            pal = color, # color palette
            values = ~Affiliated..or.Commissioning..Organization, # legend values
            opacity = 1,
            title = "Commissioning Organization")
         }) 



    # leaflet proxy map 
    observe(leafletProxy("mymap", data = filtered()) %>%
                addProviderTiles("Esri.WorldImagery") %>%
                clearMarkers() %>%
                addCircleMarkers(lng = ~Longitude,
                                 lat = ~Latitude,
                                 color = ~color(Affiliated..or.Commissioning..Organization),
                                 popup = paste("Artist:", murals$Artist.Credit, "<br>",
                                               "Title:", murals$Artwork.Title, "<br>",
                                               "Medium:", murals$Media, "<br>",
                                               "Location Description:", murals$Location.Description, "<br>",
                                               "Ward:", murals$Wards, "<br>",
                                               "Year:", murals$Year.Installed, "<br>",
                                               "Year Restored:", murals$Year.Restored))

                ) 
            }

1 个答案:

答案 0 :(得分:0)

使用pickerInput并确保选择了所有内容。由于您显示的数据集来自filtered(),并且下拉菜单中没有选择艺术家,因此它只会为您提供一个子集,该子集为空。

##################

##################

library(shiny)
library(shinythemes)
library(leaflet)
library(leaflet.extras)
library(RColorBrewer)
library(formattable)
library(dplyr)
library(stringr)

## load data ##
murals <- read.csv("https://data.cityofchicago.org/api/views/we8h-apcf/rows.csv?accessType=DOWNLOAD",
                   stringsAsFactors = F, na = c("", "N/A", "NA"))

## clean data ##

# clean Media type
murals$Media <- str_replace(murals$Media, "spray", "Spray")
murals$Media <- str_replace(murals$Media, "Spray + brush", "Spray and Brush")
murals$Media <- str_replace(murals$Media, "Painting", "Paint")
murals$Media <- str_replace(murals$Media, "LAKE VIEW", "Lake View")

# clean Titles 
murals$Artwork.Title <- str_replace(murals$Artwork.Title, "Where There Is Discord, Harmony:The Power of Art", "Where There Is Discord, Harmony: The Power of Art")


## make map easier to read with scalable circle markers ##
circle.scaler <- function(x){((x-min(x))/(max(x)-min(x)))*500}

##################

##################

# set UI
ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("mymap", width = "100%", height = "100%"),
  absolutePanel(
    top = 10,
    right = 10,

    theme = shinytheme("lumen"),
    shinyjs::inlineCSS(list(body = "color:White")),
    titlePanel("Chicago Neighborhood Murals"),

    pickerInput(
      "Artist.Credit",
      label = "Artist",
      choices = c("All",
                  unique(as.character(
                    murals$Artist.Credit
                  ))),
      selected  =  murals$Artist.Credit,
      multiple = T
    )
  )
)


##################

################## 

# set server 
server <- function(input,output, session){

  filtered <- reactive({
    murals[murals$Artist.Credit == input$Artist.Credit, ] 
  })


  # define map color markers  
  color <- colorFactor(topo.colors(3), murals$Affiliated..or.Commissioning..Organization)

  # render original leaflet map 
  output$mymap <- renderLeaflet({
    leaflet(data = murals) %>%
      addTiles() %>%
      addMarkers() %>%

      # add legend     
      addLegend(
        "bottomleft", # legend position
        pal = color, # color palette
        values = ~Affiliated..or.Commissioning..Organization, # legend values
        opacity = 1,
        title = "Commissioning Organization")
  }) 



  # leaflet proxy map 
  observe(leafletProxy("mymap", data = filtered()) %>%
            addProviderTiles("Esri.WorldImagery") %>%
            clearMarkers() %>%
            addCircleMarkers(lng = ~Longitude,
                             lat = ~Latitude,
                             color = ~color(Affiliated..or.Commissioning..Organization),
                             popup = paste("Artist:", murals$Artist.Credit, "<br>",
                                           "Title:", murals$Artwork.Title, "<br>",
                                           "Medium:", murals$Media, "<br>",
                                           "Location Description:", murals$Location.Description, "<br>",
                                           "Ward:", murals$Wards, "<br>",
                                           "Year:", murals$Year.Installed, "<br>",
                                           "Year Restored:", murals$Year.Restored))

  ) 
}



shinyApp(ui = ui, server = server)