3

I am working with leaflet package to draw maps and plot lat and long on the plot. I am doing following in R.

m <- leaflet() %>%
addTiles() %>%  # Add default OpenStreetMap map tiles
addMarkers(lng=df_final$order_long, lat=df_final$order_lat)
m

It works perfectly fine in R console. But It doesn't work when I use it in shiny This is my ui.r code snippet

tabPanel("Order Locations", leafletOutput("map", width = "80%", height =  
"80%"))

and this is server.r code snippet

 output$map<- renderLeaflet({

 dataset<-dataUpload()
 leaflet() %>%
 addTiles() %>%  # Add default OpenStreetMap map tiles
 addMarkers(lng=dataset$order_long,lat=dataset$order_lat)
 })

It doesn't display anything. Where I am getting wrong. please help

Neil
  • 7,937
  • 22
  • 87
  • 145

3 Answers3

11

This works, I made it as close to yours as possible. It doesn't like percentage heights apparently:

library(shiny)
library(leaflet)

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

ui <- fluidPage(
  actionButton("recalc", "New points"),
  mainPanel(
    tabsetPanel(
      tabPanel("Order Locations", leafletOutput("map",width="80%",height="400px")),
      tabPanel("Markers", verbatimTextOutput("markers"))
    )
  )
)

if (!file.exists("df_final.csv")){
  nmark <- 50
  hv <- 80
  df_final <- data.frame(order_long=runif(nmark,-hv,hv),
                         order_lat=runif(nmark,-hv,hv))
  write.csv(df_final,"df_final.csv",row.names=F)
}

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

  dataUpload <- reactiveFileReader(1000, session, 'df_final.csv', read.csv)

  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      addMarkers(lng=dataUpload()$order_long,lat=dataUpload()$order_lat)
  })
  output$markers <- renderPrint({print(dataUpload())})
}

shinyApp(ui, server)

Yielding:

enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
2

It Worked. Problem was with the ui.r file.

This is the modified code

tabPanel("Order Locations", leafletOutput("map"))

got rid off width and height parameters and it worked with default settings.

Neil
  • 7,937
  • 22
  • 87
  • 145
1

Somewhat late to the party, but you can add your own CSS to control leaflet's aesthetics, typically in a www folder, e.g. www/styles.css, which has the added benefit of responding to changes to the size of the window.

  /* styles.css */
  div.outer {
  position: fixed;
  top: 45px; /* set to fit nicely with Shinydashboard's dashboardHeader(), adjust according to your own preferences. */
  left: 0;
  right: 0;
  bottom: 0;
  overflow-y: auto;
  padding: 0;
}

and add this class in a div() in your ui:

ui <- fluidPage(
 actionButton("recalc", "New points"),
 mainPanel(
  tabsetPanel(
   tabPanel("Order Locations", 
            div(
             class = "outer",
             tags$head(
              includeCSS("www/styles.css")
             ),
             leafletOutput("map",width="100%",height="100%")
             )
            ),
   tabPanel("Markers", verbatimTextOutput("markers"))
  )
 )
)

The result: enter image description here

msoderstrom
  • 537
  • 1
  • 7
  • 14