Publishing a Shiny/Leaflet Map made in R to a Website
As the number of breweries in Connecticut (CT) continues to grow, it’s essential to visualize this data for better understanding and analysis. In this article, we’ll explore how to publish a Shiny/Leaflet map created in R to a website.
Introduction
Shiny is an R package that allows users to create web applications using R code. Leaflet is a JavaScript library used in conjunction with Shiny to create interactive maps. We’ll walk through the process of creating a Shiny application, publishing it to a website, and explore potential issues that may arise during this process.
Prerequisites
To follow along with this article, you should have:
- R installed on your system
- A basic understanding of R programming language
- Familiarity with the Shiny package for creating web applications
- Experience with Leaflet and its integration with Shiny
Creating a Shiny Application
First, we’ll create a simple Shiny application that displays a map of breweries in CT using Leaflet.
# Install necessary packages
install.packages("shiny")
install.packages("leaflet")
# Load required libraries
library(shiny)
library(leaflet)
# Define UI for the application
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", width = "100%", height = "100%"),
absolutePanel(bottom = 30, right = 10,
textOutput("Counter"),
sliderInput("Year", "Year", 1990, 2000, value = 1995, step = 1, sep = "")
)
)
# Define server-side logic for the application
server <- function(input, output, session) {
# Load required libraries and datasets
library(leaflet.extras)
library(fontawesome)
library(rsconnect)
ct_breweries <- read.csv('ct_breweries.csv', header=TRUE, sep=',')
# Create reactive expression for filtering data based on year
sliderData <- reactive({
ct_breweries %>%
filter(YearOpened <= input$Year)
})
# Output counter text
output$Counter <- renderText({
paste('Number of Breweries: ', nrow(sliderData()))
})
# Output leaflet map
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
fitBounds(min(ct_breweries$Longitude), min(ct_breweries$Latitude),
max(ct_breweries$Longitude), max(ct_breweries$Latitude))
})
# Observe changes to the slider input and update map accordingly
observe({
leafletProxy("map", data = sliderData()) %>%
clearMarkers() %>%
addProviderTiles(provider = 'Esri.WorldStreetMap') %>%
addAwesomeMarkers(icon = beer_icon,
group = ~ Name)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Publishing to ShinyApps.io
To publish our Shiny application to ShinyApps.io, we need to create a new shiny app and upload our code.
# Create new shiny app on shinyapps.io
# Go to shinyapps.io and click "New App"
# Enter your app name and select R as the programming language
# Upload your ui.R and server.R files
Publishing to Website
Instead of publishing to ShinyApps.io, we can publish our application directly to a website. To do this, we’ll use the rsconnect package to create an rsConnect R server and upload our code.
# Install required libraries
install.packages("rsconnect")
library(rsconnect)
# Create new rsConnect app
rsApp <- rsApp("Breweries Map", "A map of breweries in CT")
# Upload ui.R file
ui <- source_file("ui.R")
# Upload server.R file
server <- source_file("server.R")
# Start the rsConnect server
rsApp(app = server)
Potential Issues
One common issue that may arise when publishing a Shiny application is “Disconnected from Server” error. This can occur if the dataset used in the application is not visible to the server.
To fix this, we need to load all necessary packages and datasets into a global R file (in the same app folder). We should also ensure that the csv file is in the same folder as our shiny app.
# Install required libraries
install.packages("shiny")
install.packages("leaflet")
library(shiny)
library(leaflet)
# Load required libraries and datasets
ct_breweries <- read.csv('ct_breweries.csv', header=TRUE, sep=',')
Alternative Solution
Instead of publishing the map to a website, we can save an HTML file of the map that our friend can open in a browser. We’ll use the htmlwidgets package to create an HTML widget from our leaflet map.
# Install required libraries
install.packages("leaflet")
library(leaflet)
library(htmlwidgets)
# Load dataset
ct_breweries <- read.csv('ct_breweries.csv', header=TRUE, sep=',')
# Create leaflet map
leaflet_map <- leaflet(data = ct_breweries) %>%
addProviderTiles(provider = 'Esri.WorldStreetMap') %>%
addAwesomeMarkers(icon = beer_icon,
group = ~ YearOpened,
popup = ~ Name)
# Save HTML file of the map
saveWidget(leaflet_map, file="leaflet_map.html")
This solution will result in an offline HTML file that can be shared with our friend. However, this may not meet all the requirements of our original application.
Conclusion
Publishing a Shiny/Leaflet map made in R to a website requires careful consideration of dataset visibility and server-side logic. By following these steps and potential solutions, we can successfully publish our application to a website or share an offline HTML file with others.
Last modified on 2025-03-27