Shiny Dashboards
Creating interactive web applications with shiny
allows you to dynamically connect data with visuals, enabling users to interact with your analysis in real time. This topic introduces the core components of shiny
—inputs, outputs, and reactive expressions—to build responsive, dynamic applications.
1. Introduction to shiny
shiny
is an R package that simplifies building interactive web applications directly from R. A basic shiny
app consists of two main components:
- UI (User Interface): Defines the layout and appearance of the app.
- Server: Contains the logic for processing inputs and generating outputs.
Here’s a quick overview of how shiny
apps work:
- Users interact with UI elements (like sliders or dropdowns).
- Inputs are processed in the server function.
- Outputs are dynamically updated and displayed in the UI.
Install and Load shiny
r
Copy codeinstall.packages("shiny")
library(shiny)
2. Using shiny
Inputs and Outputs to Enhance Dashboards
Inputs and outputs form the backbone of any shiny
application. Let’s explore how to create interactive apps with these components.
Common Input Widgets
sliderInput
: A slider for numeric ranges.selectInput
: A dropdown menu.textInput
: A text box for user input.checkboxInput
andcheckboxGroupInput
: Checkboxes for single or multiple selections.
Example: A Simple Reactive Histogram
r
Copy codelibrary(shiny)
# Define the UI
<- fluidPage(
ui titlePanel("Interactive Histogram"),
sidebarLayout(
sidebarPanel(
sliderInput("bins", "Number of bins:",
min = 5, max = 50, value = 30)
),mainPanel(
plotOutput("histPlot")
)
)
)
# Define the server logic
<- function(input, output) {
server $histPlot <- renderPlot({
outputhist(rnorm(500), breaks = input$bins,
col = "skyblue", border = "white")
})
}
# Run the application
shinyApp(ui = ui, server = server)
Explanation:
sliderInput
: Lets the user select the number of bins for the histogram.renderPlot
: Dynamically updates the histogram based on user input.hist()
: Generates the plot, with the number of bins controlled by the slider.
3. Reactive Expressions for Dynamic Updates
Reactive expressions are the core of shiny
’s reactivity. They allow you to efficiently manage computations that depend on user inputs, ensuring that only the necessary components update when inputs change.
Creating Reactive Expressions
A reactive expression is defined using reactive()
and recalculates its value only when its dependencies change.
Example: Reactive Data Transformation
r
Copy codelibrary(shiny)
# UI definition
<- fluidPage(
ui titlePanel("Reactive Data Transformation"),
sidebarLayout(
sidebarPanel(
sliderInput("threshold", "Filter Threshold:",
min = 0, max = 100, value = 50)
),mainPanel(
plotOutput("scatterPlot"),
tableOutput("filteredTable")
)
)
)
# Server logic
<- function(input, output) {
server # Reactive expression for filtering data
<- reactive({
filteredData $mpg > input$threshold, ]
mtcars[mtcars
})
# Render scatter plot
$scatterPlot <- renderPlot({
outputplot(filteredData()$wt, filteredData()$mpg,
main = "Filtered Data",
xlab = "Weight", ylab = "MPG",
col = "blue", pch = 19)
})
# Render filtered data table
$filteredTable <- renderTable({
outputfilteredData()
})
}
# Run the application
shinyApp(ui = ui, server = server)
Explanation:
reactive
: DefinesfilteredData
, which automatically updates wheninput$threshold
changes.renderPlot
andrenderTable
: UsefilteredData()
(note the parentheses to call the reactive expression) to generate updated outputs.
4. Best Practices for Building Interactive Apps
- Use reactive expressions wisely: Avoid duplicating calculations by centralizing logic in a reactive expression.
- Organize your code: Separate UI and server logic into different files for complex apps.
- Add validation: Use functions like
validate()
andreq()
to ensure inputs are valid before processing. - Minimize reactivity: Use
observeEvent()
when you don’t need reactive outputs but still want to respond to user input.
Summary
In this topic, you learned how to:
- Use inputs (like sliders and dropdowns) and outputs (like plots and tables) to create interactive web applications with
shiny
. - Apply reactive expressions to manage dynamic updates efficiently and avoid redundant computations.
- Build simple yet powerful applications that connect data with visuals for real-time exploration.