3.1.7 mainpanel: LeafletOutput
leafletOutput(outputId = "map", width = '100%', height = '1500px')
A Shiny app in R is a web application framework that allows users to create interactive and dynamic web apps directly from R without needing extensive web development skills. Shiny combines the power of R’s data analysis and visualization capabilities with an easy-to-use interface for building responsive, interactive applications.
Key important of every Shiny app: 1. UI (short for user interface) which defines how your app looks 2. server function: defines how your app works 3. reactive programming:automatically update outputs when inputs change
if (!require('shiny')) install.packages('shiny')
library(shiny)
Create a new directory and an app.R file containing a basic app in one step by clicking File | New Project | Shiny Web Application.
There are a few ways you can run this app:
127.0.0.1 is a standard address that means “this computer” and 7704 is a randomly assigned port number.
In reactive programming, the main idea is to create a network of connections between inputs and outputs. When an input changes, all related outputs are updated automatically. This makes the app’s behavior easier to manage, but it can take some time to fully understand how everything works together
Server functions take three parameters: input
,
output
.
server = function(input, output){
}
The input argument is a list-like object that contains all the input data sent from the browser, named according to the input ID.
One important thing to know about input is that it can only be
accessed in specific situations. To read the value of an input, you need
to be inside a reactive function, like renderText()
or reactive()
.
These functions make sure the app reacts to changes in the input.
reactive: Create a reactive expression
In Shiny, a reactive expression is a special type of function that automatically updates its value when the inputs it depends on change. It helps manage dynamic behavior in Shiny apps.
filtered_data <- reactive({
tf_data %>%
filter(Mortgage >= input$mortgageRange[1],
Mortgage <= input$mortgageRange[2],
State %in% input$stateFilter)
})
The output object works similarly to input: it’s a list-like object where each item is named according to the outputId. The difference is that output is used to display information (like text, tables, or plots) rather than receive input. You always use the output object along with a render* function to create the output.
# Render the leaflet map
output$map <- renderLeaflet({
leaflet(height = 500) %>%
addTiles() %>%
setView(lng = mean(st_coordinates(tf_data)[,1]),
lat = mean(st_coordinates(tf_data)[,2]),
zoom = 6)
})
A reactive function in Shiny that watches for changes in any reactive input or data (e.g., filtered_data() in this case). When the observed data changes, the code inside observe() is executed automatically. It is used to create dynamic behaviors in a Shiny app.
leafletProxy() is used to modify an existing Leaflet map without redrawing it from scratch. The first argument is the ID of the map (“map”), and the second argument is the data (filtered_data()). The filtered data will be used to update the markers on the map.
observe({
leafletProxy("map", data = filtered_data()) %>%
clearMarkers() %>%
addCircleMarkers(
radius = 3, # Adjust size based on Mortgage value (adjust the divisor as needed)
color = "blue",
stroke = FALSE,
fillOpacity = 0.5,
label = ~paste("Mortgage:", Mortgage, "<br>", "State:", State)
)
})
Click the Run App button in the document toolbar. ‘Select Run external’ to display the app in external browser.