The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.
This vignette will walk through creating a 4 tab leaflet-based Shiny App of New York state biodiversity for an outdoor enthusiast. Point, polyline, and polygon data should be in a leaflet-compatible format prior to proceeding with the following steps for your own project.
Provided datasets for this vignette come from https://data.ny.gov/, with
the exception of counties_NY which was accessed through the
tigris
package and then combines with the biodiversity
data. Preprocssed datasets include:
Polygons:
counties_NY (via tigris package)
amphibians
birds
flowering_plants
reptiles
Points:
points_campgrounds
points_parks
points_watchsites
Polylines:
roads_ny_interstate
Taking a look at a few of the datasets:
head(points_campgrounds)
#> label long lat
#> 1 Lakeside Beach -78.24370 43.36638
#> 2 Letchworth -77.97577 42.64592
#> 3 Long Point -76.69841 42.71741
#> 4 Long Point -76.21706 44.02844
#> 5 Macomb Reservation -73.61241 44.61786
#> 6 Margaret Lewis Norrie -73.93896 41.84154
First, we create one list per type of spatial data for each tab we need in the final AtlasMaker Shiny App. We’ll make four for this vignette: Flowering Plants, Birds, Amphibians & Reptiles, and All.
For our first Flowering Plants tab, we create one list for the needed polygons and provide the name to be referenced, the datafile which is already in a spatial dataframe format, and then provide which variable should be used for the label (‘name’) and which variable should be used for the polygon fill (‘fill_value’). In this example, the fill_value is the biodiversity of flowering plants in the given New York state county.
## polygons list for tab 1: flowering plants-------------
polys_flowering_plants <- list(
list(
name = 'flowering_plants',
data = flowering_plants,
label = 'name',
fill = 'fill_value'
)
)
With just the list above, the Flowering Plant tab would display New York state with shaded counties based on the biodiversity fill_value. Let’s add some points to the map as well. With the list below, we name and choose the spatial dataframe with point data of where parks are in New York. Next, we map which variables in the spatial dataframe correspond with the long, lat, and label.
Our next tab for Birds is quite similar, but we add a polylines list
to show where New York interstates run with the lines_birds
list. We also need two sets of points to display both campgrounds and
wildlife watchsites, and these are nested into the larger
points_birds
list.
## polygon list for tab 2: birds-------------
polys_birds <- list(
list(
name = 'birds',
data = birds,
label = 'name',
fill = 'fill_value'
)
)
## polyline list for tab 2: birds-------------
lines_birds <- list(
list(
name = 'ny_interstates',
data = roads_ny_interstate
)
)
## points list for tab 2: birds-------------
points_birds <- list(
list(
name = 'points_watchsites',
data = points_watchsites,
long = 'long',
lat = 'lat',
label = 'label'
),
list(
name = 'points_campgrounds',
data = points_campgrounds,
long = 'long',
lat = 'lat',
label = 'label'
)
)
For Tab 3 we decide to show both our amphibian and reptiles data, so
polys_amp_rep
is a nested list. Tab 4 will display all the
biodiversity data, so has an even larger nested list for it’s polygon
list.
## polygon list for tab 3: amph & rept -------------
polys_amph_rept <- list(
list(
name = 'amphibians',
data = amphibians,
label = 'name',
fill = 'fill_value'
),
list(
name = 'reptiles',
data = reptiles,
label = 'label',
fill = 'fill_value'
)
)
## point list for tab 3: amph & rept-------------
points_amp_rept <- list(
list(
name = 'points_parks',
data = points_parks,
long = 'long',
lat = 'lat',
label = 'label'
),
list(
name = 'points_campgrounds',
data = points_campgrounds,
long = 'long',
lat = 'lat',
label = 'label'
)
)
## for tab 4-------------
## polygons list for tab 4: all-------------
polys_all <- list(
list(
name = 'flowering_plants',
data = flowering_plants,
label = 'name',
fill = 'fill_value'
),
list(
name = 'birds',
data = birds,
label = 'name',
fill = 'fill_value'
),
list(
name = 'amphibians',
data = amphibians,
label = 'name',
fill = 'fill_value'
),
list(
name = 'reptiles',
data = reptiles,
label = 'label',
fill = 'fill_value'
)
)
## points list for tab 4: all-------------
points_all <- list(
list(
name = 'points_parks',
data = points_parks,
long = 'long',
lat = 'lat',
label = 'label'
),
list(
name = 'points_campgrounds',
data = points_campgrounds,
long = 'long',
lat = 'lat',
label = 'label'
)
)
As usual with Shiny, we set up a basic ui
for the app.
For each tabPanel()
the title to display goes in quotes
first, followed by the function map_UI()
with an id you
create for each map.
# 3. Set ui/layout --------
ui <- fluidPage(
titlePanel("AtlasMaker Demo Map (v0.9)"),
mainPanel(
tabsetPanel(
tabPanel('Flowering Plants', map_UI('flowering_plants')),
tabPanel('Birds', map_UI('birds')),
tabPanel('Amphibians & Reptiles', map_UI('amph_rept')),
tabPanel("All", map_UI("allthegoods"))
)
)
)
The server
is where AtlasMaker uses the
map_server()
function to quickly fill in the lists we’ve
created above.
Using one map_server call per tab and starting with the ‘flowering_plants’, we start with the id created in the UI, followed by setting the polygons and points arguments to the lists we created above.
Set a leaflet supported poly_palette, point_color, polyline_color, and map_base_theme if desired.
server <- function(input, output) {
map_server("flowering_plants",
polygons = polys_flowering_plants,
polygons_legend_title = "Biodiversity Count",
polylines = NULL,
points = points_flowering_plants,
poly_palette = 'RdPu',
point_color = 'brown'
)
map_server("birds",
polygons = polys_birds,
polylines = lines_birds,
points = points_birds,
map_base_theme = 'Stamen.Watercolor',
poly_palette = 'YlGn',
point_color = '#ffa500',
polyline_color = "#964b00"
)
map_server("amph_rept",
polygons = polys_amph_rept,
polylines = NULL,
points = points_amp_rept,
map_base_theme = 'Esri.WorldImagery',
poly_palette = 'Greens',
point_color = "black"
)
map_server("allthegoods",
polygons = polys_all,
polylines = NULL,
points = points_all
)
}
See the ‘colors’ section of Leaflet guide for options to pass in for colors/palettes: http://rstudio.github.io/leaflet/colors.html
Pass in any base layer from the link into ‘map_base_theme’: http://leaflet-extras.github.io/leaflet-providers/preview/index.html
Please note: knitting this Markdown file will only display a static image of the app, run the following code chunk in your R session to see the fully interactive demo app.
The demo app is admittedly a little messy style wise, but with the
purpose to show how easily the aesthetics of the maps can be changed
using the map_server()
arguments.
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.