Search code examples
rshinyr-leaflet

How to display a spatial dataframe with renderTable


I am building a Shiny app with a Leaflet map based on a PostgreSQL spatialdatabase.

I succeeded to import spatial data into SpatialPolygonDataFrame and to display it on Leaflet widget.

I am trying to display the data from the SpatialDataFrame with a RenderTable output, but its not working, even by converting it with as.data.frame(spatialdataframe). Therefore this conversion is enough to dispay the table with view(), kable() or other display functions, but not in Shiny.

Should I make another conversion? Anyone got an idea?

    ui <- fluidPage(
titlePanel("AgriPAG"),
sidebarLayout(
    mainPanel(
        tabsetPanel(
            tabPanel(leafletOutput("m",width = "100%", height = 1000)),
            tabPanel(tableOutput(as.data.frame(sample_test1)))
        )
    ),
    sidebarPanel("curseur")
)
)

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

data <- reactive({
x <- test1
})

output$mymap <- renderLeaflet({
test1 <- data()

m <- leaflet(data = sample_test1) %>%
        addTiles() %>%
        setView(lng=-52.3333300, lat=4.9333300 , zoom=10) %>%
        addPolygons(data=sample_test1, weight=2, col="black", opacity=0.5)
m
})

output$table <- renderDataTable(as.data.frame(sample_test1))

}

shinyApp(ui = ui, server = server)

Solution

  • renderDataTable does not work with tableOutput. You have to use dataTableOutput instead. In addition, you should add the correct inputId for dataTableOutput.

    To get everything to work change: tableOutput(as.data.frame(sample_test1)) in your ui to dataTableOutput('table')