2016-10-16 24 views
2

我使用viridis調色板繪製圖例的顏色時出現問題:雖然圖例標籤是顏色,但不顯示顏色。Shiny Server:leafet在圖例中不顯示viridis顏色

enter image description here

我測試Ubuntu的下相同的代碼與Shiny Server v1.4.2.786Node.js v0.10.40(它不顯示貽貝顏色)和MacOS下(它正確一樣)。

Ubuntu的R對話的細節:

R version 3.3.1 (2016-06-21) 
Platform: x86_64-pc-linux-gnu (64-bit) 
Running under: Ubuntu 15.10 

leaflet_1.0.1 shiny_0.13.2 viridis_0.3.4 

這是不顯示的顏色

leaflet() %>% addTiles() %>% addLegend(
     position = 'bottomright', 
     colors = viridis(8), 
     labels = viridis(8), opacity = 1) 

而這個作品也Ubuntu的機器上的傳奇

leaflet() %>% addTiles() %>% addLegend(
     position = 'bottomright', 
     colors = rgb(t(col2rgb(palette()))/255), 
     labels = palette(), opacity = 1) 

enter image description here

它真的好像是viridis調色板的顏色代碼有問題(我嘗試在一個字符矢量中複製/粘貼它們)。

工作示例

library(shiny) 
library(leaflet) 
library(viridis) 

r_colors <- rgb(t(col2rgb(colors())/255)) 
names(r_colors) <- colors() 

ui <- fluidPage(
    leafletOutput("mymap") 
) 

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

    output$mymap <- renderLeaflet({ 
    leaflet() %>% addTiles() %>% addLegend(
     position = 'bottomright', 
     colors = viridis(8), 
     labels = viridis(8), opacity = 1) 

    }) 
} 

shinyApp(ui, server) 

回答

0

我運行Ubuntu機14.04LTS。我能夠獲得圖例上的顏色,但它看起來像colors()函數中沒有列出顏色,對於圖例標籤仍然是十六進制代碼。

這部分代碼應檢索顏色名稱:

colors()[match(rgb(t(col2rgb(leafletColors)), 
         maxColorValue = 255), c(rgb(t(col2rgb(colors())), maxColorValue = 255)))] 

修改app.R代碼

library(shiny) 
    library(leaflet) 
    library(viridis) 

    r_colors <- rgb(t(col2rgb(colors())/255)) 
    names(r_colors) <- colors() 
    leafletColors <- palette(viridis(8)) 

    ui <- fluidPage(
      leafletOutput("mymap"), 
      p(), 
      actionButton("recalc", "New points") 
    ) 

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

      points <- eventReactive(input$recalc, { 
        cbind(rnorm(40) * 2 + 13, rnorm(40) + 48) 
      }, ignoreNULL = FALSE) 

      output$mymap <- renderLeaflet({ 
        leaflet() %>% 
          addProviderTiles("Stamen.TonerLite", 
              options = providerTileOptions(noWrap = TRUE) 
          ) %>% 
          addMarkers(data = points()) %>% 
          addLegend(
            position = 'bottomright', 
            colors = leafletColors, 
            labels = palette(), opacity = 1) 
      }) 
    } 

    shinyApp(ui, server) 

讓我知道這是有益的...

1

這有與viridis調色板的alpha通道#xxxxxxFF有關。 viridismapview包的默認調色板時,我有這個相同的問題。我寫了一個小函數來解決這個問題。該函數不會導入到名稱空間,因此只能通過mapview:::col2Hex訪問它。它定義爲:

function(col, alpha = FALSE) { 

    mat <- grDevices::col2rgb(col, alpha = TRUE) 
    if (alpha) { 
    hx <- grDevices::rgb(mat[1, ]/255, mat[2, ]/255, 
         mat[3, ]/255, mat[4, ]/255) 
    } else { 
    hx <- grDevices::rgb(mat[1, ]/255, mat[2, ]/255, mat[3, ]/255) 
    } 
    return(hx) 

} 

和來源可以找到here

這樣,你的代碼應該工作。

leaflet() %>% addTiles() %>% addLegend(
    position = 'bottomright', 
    colors = mapview:::col2Hex(viridis(8)), 
    labels = mapview:::col2Hex(viridis(8)), opacity = 1) 

嘗試設置阿爾法TRUE和你結束了無顏色:單張

leaflet() %>% addTiles() %>% addLegend(
    position = 'bottomright', 
    colors = mapview:::col2Hex(viridis(8), alpha = TRUE), 
    labels = mapview:::col2Hex(viridis(8), alpha = TRUE), opacity = 1)