Search code examples
rshinyr-leaflet

renderLeaflet is not working in shiny app


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


Solution

  • 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