Mickey_NC Mickey_NC - 1 month ago 25
R Question

R Shiny, how to display a shapefile (map) in a tab?

I'm new to R and shiny package.

I have created a map with tmap and gdal packages and I woud like to know how to display it in a shiny tab ?

Which is the command that allows it ?

Thank you very much.

Have a great day.

Ok, so, this is what I did with maptools package, but there's nothing in the tab :

server : shpFile <- readShapePoly("c:\shapefiles\counties.shp")
output$map <- renderPlot({shpFile})


ui : tabPanel("Map",
mainPanel(plotOutput("map"))
)


So, if you see where is the problem, thank you for helping me.

EDIT : Ok, I found it.

This is the Server.R code :

output$SHPplot <- renderPlot({
lnd <- readOGR(dsn = "//test/Shapefiles",
layer = "Countries")

tm_shape(lnd) +
tm_fill("COUNTRIES", title = "GDP", style = "fixed",
breaks = c(60, 70, 80, 90, 100),
# palette = c("red", "yellow", "blue", "turquoise")) +
# col=c("#edf8fb", "#b2e2e2", "#662a4", "238b45")) +
palette = "YlOrRd") +
tm_borders() +
tm_layout("Wealth (or so)",
legend.title.size = 1,
legend.text.size = 0.6,
legend.position = c("left","bottom"),
legend.bg.color = "white",
#legend.digits = 5,
legend.bg.alpha = 1)
})


And Ui.R code :

plotOutput("SHPplot", height = 675)


Thanks for helping.

Answer

Shiny works with two main elements: server.R and ui.R. If you want to display a map, you could use this structure:

server.R

output$plot_map <- renderPlot({

  • put the function which makes your map here *

})

ui.R

plotOutput("plot_map")

Comments