如何让传单图美化闪亮?

问题描述 投票:0回答:1

我**终于**让下拉菜单正常工作,但有一些挥之不去的问题:

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

  • 在我的用户界面中,当我添加更多 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))
                
                ) 
            }
r shiny r-leaflet shiny-reactivity
1个回答
1
投票

使用 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)
© www.soinside.com 2019 - 2024. All rights reserved.