【问题标题】:Changing pallete values based on input (Shiny & leaflet)根据输入更改调色板值(闪亮和传单)
【发布时间】:2020-05-09 07:08:07
【问题描述】:

我正在使用传单和闪亮。我想根据可以通过输入更改的列为我的标记着色。它与Modifying Existing Maps with leafletProxy 几乎相同。在此示例中,用户可以更改调色板。在我的示例中,我想更改应用调色板的列。 我正在尝试使用类似的东西:

fillColor = ~pal(!!sym(input$col_to_apply)) # input$col_to_apply is a column name (string) I want to use

但是,这不起作用。我也不确定在这种情况下是否必须使用reactive()

【问题讨论】:

    标签: shiny leaflet


    【解决方案1】:

    当然。我的建议是在创建调色板之前进行。调色板已经够棘手了。请参阅下面的最小示例:

    library(leaflet)
    library(maps)
    library(shiny)
    
    ui <- fluidPage(
    
        leafletOutput("map_1"),
    
        selectInput(inputId = "input_species", label = "Species Selection", choices = c("Species_1", "Species_2", "Species_3"))
    
    )
    
    
    server <- function(input, output, session) {
    
        #Load a map of the US from the 'map' package (runs once when apps starts)
        shp_map = map("state", fill = TRUE, plot = FALSE)
    
        #Make up a dataframe with some data for three species for each state (runs once when apps starts)
        df_data <- data.frame(state = unique(shp_map$names), Species_1 = sample(100:199, 63), Species_2 = sample(200:299, 63), Species_3 = sample(300:399, 63))
    
        #Create map
        output$map_1 <- renderLeaflet({
    
            df_map <- df_data
    
            #Create a column called species selected based on which is selected in the dropdown box
            df_map$Species_Selected <- df_map[, paste(input$input_species)]
    
            #Create a palette function
            palette <- colorNumeric(palette = "Blues", domain = df_map$Species_Selected)
    
            #Use the palette function created above to add the appropriate RGB value to our dataframe
            df_map$color <- palette(df_map$Species_Selected)
    
            #Create map
            map_1 <- leaflet(data = shp_map) %>% 
    
                addPolygons(fillColor = df_map$color, fillOpacity = 1, weight = 1, color = "#000000", popup = paste(sep = "", "<b>", paste(shp_map$names), " ", "</b><br>", df_map$Species_Selected)) 
    
            map_1
    
        })
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-29
      • 2020-11-15
      • 2021-03-09
      • 1970-01-01
      • 2021-10-08
      • 2021-04-18
      • 2018-12-10
      • 2018-06-15
      相关资源
      最近更新 更多