Control the size of popupImage from leaflet in r s

2020-03-29 05:52发布

问题:

I am having difficulties to control the size of the image resulting from popupImage (mapview package). Below is a reproducible shiny example where I have a single marker with a popup. When I set width = 300, the popup displays correctly, but I would like to display a bigger image.
width = 300

When setting width = 500, the popup displays larger, but a portion of it is greyed out and a scroll bar is added.
width=500.

How can I get width = 500 to display the image correctly?

I've messed around with css tags, worked through everything I could find on stackoverflow, and examined the GitHub documents.

library(shiny)
library(leaflet)
library(dplyr)
library(mapview)

img = "https://cdn.sstatic.net/Sites/stackoverflow/img/error-lolcat-problemz.jpg"

ui <- fluidPage(
  titlePanel("example"),
  sidebarLayout(
    sidebarPanel(width=2),# closes sidebar panel
  mainPanel(
    tags$style(type = "text/css", "#map {height: calc(85vh) !important;}"),
    leafletOutput(outputId = "map") 
))) 

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

output$map <- renderLeaflet({
  leaflet() %>% setView(lng= -96.83875, lat = 29.58518, zoom = 9)%>%
    addProviderTiles("Stamen.Toner") %>%
    addMarkers(lng= -96.83875, lat = 29.58518, popup = popupImage(img, embed= TRUE, width = 300))
  })}  

# Run app ----
shinyApp(ui, server)

回答1:

The div-wrapper where the popup is, has a width of 301px as default apparently. You can change it with some css.

library(shiny)
library(leaflet)
library(dplyr)
library(mapview)

img = "https://cdn.sstatic.net/Sites/stackoverflow/img/error-lolcat-problemz.jpg"

csscode = HTML("
.leaflet-popup-content {
  width: 500px !important;
}")

ui <- fluidPage(
  titlePanel("example"),
  tags$head(tags$style(csscode)),
  sidebarLayout(
    sidebarPanel(width=2),# closes sidebar panel

    mainPanel(
      tags$style(type = "text/css", "#map {height: calc(85vh) !important;}"),
      leafletOutput(outputId = "map") 
    ))) 

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

  output$map <- renderLeaflet({
    leaflet() %>% setView(lng= -96.83875, lat = 29.58518, zoom = 9)%>%
      addProviderTiles("Stamen.Toner") %>%
      addMarkers(lng= -96.83875, lat = 29.58518,popup=popupImage(img,embed=TRUE,width=500))
  })}  

# Run app ----
shinyApp(ui, server)