I am trying to plot a map in shiny using leaflet, but I keep getting an error that I don't understand. Below is a minimum reproducible example. Thanks in advance for your help
library(shiny)
library(leaflet)
ui = fluidPage("test", id="nav",
leafletOutput("map", width="100%", height="100%")
)
server <- function(input, output,session) {
output$map <- renderLeaflet({
print("Rendering leaflet map")
leaflet() %>% addProviderTiles("Esri.OceanBasemap") %>%
fitBounds(160, -30, 185, -50)
print("Finishing rendering leaflet map")
})
}
shinyApp(ui, server);
I get this printed to the console:
[1] "Rendering leaflet map"
[1] "Finishing rendering leaflet map"
Warning: Error in $: $ operator is invalid for atomic vectors
Stack trace (innermost first):
80: origRenderFunc
79: output$map
4: <Anonymous>
3: do.call
2: print.shiny.appobj
1: <Promise>
The leaflet function seems to work when outside of the shiny framework hence why I am a little confused.
leaflet() %>% addProviderTiles("Esri.OceanBasemap") %>%
fitBounds(160, -30, 185, -50)
A couple of points
renderLeaflet()
call is what is returned. So, if you have a print
statement, that is what is returned, not the leaflet objectshinyUI()
and shinyServer()
wrapped around your UI and Server functionsheight
is a tricky argument to get right - see this threadlibrary(shiny)
library(leaflet)
ui <- fluidPage(
leafletOutput(outputId = "map", width="100%")
)
server <- function(session, input, output) {
output$map <- renderLeaflet({
leaflet() %>% addProviderTiles("Esri.OceanBasemap") %>%
fitBounds(160, -30, 185, -50)
})
}
shinyApp(ui, server);