Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

legend labels not displaying inline when using labels and colors in leaflet map on shiny

When i want to use labels and colors parameters with addLegend() function inside a shinyAppthe legend is displayed in staircase as you can see below. But if i render the map only with leaflet outside of the shinyAppthe labels are correctly displayed inline.
I have seen this post with the same issue but their is no reproductible example so i decided to post my own question.

  • Wrong Display (shiny dashboard)

wrongDisplay

  • Correct Display (leaflet standalone)

correctDisplay

I made a reproductible example :

# ----- Load and install missing packages
packages<-c("shiny","shinydashboard","leaflet")
new.packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
lapply(packages, require, character.only = TRUE)
rm(list = c("new.packages","packages"))

# ----- Reproductible Example

# ----- UI
header <- dashboardHeader(title = "Repoductible Example")
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("map", tabName = "map", icon = icon("globe",lib="font-awesome"))
  )
)
body <- dashboardBody(
  tabItems(
    tabItem(tabName= "map",
            column(width=12,
                   leafletOutput("mapExmpl", width="100%",height=600)))
  )
)

ui <- dashboardPage(header, sidebar, body,skin="blue")

# ----- Server
server <- function(input, output) {
  labels=c("Label1","Label2","Label3","Label4","Label5")
  colors<-c(rgb(243,87,26,maxColorValue=256)
            ,rgb(225,205,19,maxColorValue=256)
            ,rgb(62,3,79,maxColorValue=256)
            ,rgb(17,126,147,maxColorValue = 256)
            ,rgb(61,255,80,maxColorValue=256))
  output$mapExmpl<-renderLeaflet({
    leaflet()%>%addTiles(
    )%>%
      addLegend("bottomright", colors = colors, labels =labels ,
                title = "Typo",
                opacity = 1
      )
  })


}

shinyApp(ui,server)
like image 588
Christophe D. Avatar asked Jul 05 '16 07:07

Christophe D.


2 Answers

I had the same problem. In my case, adjusting CSS of the legend solved the problem:

ui <- bootstrapPage( 
  tags$style(type="text/css", "div.info.legend.leaflet-control br {clear: both;}"),
...
)
like image 112
Adam Avatar answered Oct 07 '22 13:10

Adam


One of the reason it may happen is when the web-page is zoomed in i.e. the zoom level is more than 100%. Make sure you are not zoomed in. Press Control + 0 from your keyboard to reset zoom to 100%. Also, try using another web-browser if problem persists.

I had the same silly problem since my browser was zoomed in (>100%).

like image 30
Stat-R Avatar answered Oct 07 '22 13:10

Stat-R