Interactive Dashboards
Static charts tell a story once. Shiny and Plotly let your audience explore the data themselves — filtering, zooming, and interacting with live dashboards built entirely in R.
1. What is Shiny?
Shiny is an R package for building interactive web applications directly from R code — no HTML, CSS, or JavaScript required (though you can add them for polish).
install.packages("shiny")
library(shiny)
2. Anatomy of a Shiny App
Every Shiny app has two core parts: a UI (what the user sees) and a server function (the logic that responds to input).
library(shiny)
ui <- fluidPage(
titlePanel("Simple Shiny Dashboard"),
sidebarLayout(
sidebarPanel(
sliderInput("bins", "Number of bins:", min = 5, max = 50, value = 20)
),
mainPanel(
plotOutput("histPlot")
)
)
)
server <- function(input, output) {
output$histPlot <- renderPlot({
hist(rnorm(500), breaks = input$bins,
col = "steelblue", main = "Live Histogram")
})
}
shinyApp(ui = ui, server = server)
3. Key UI Input Widgets
| Function | Widget |
|---|---|
| sliderInput() | Numeric slider |
| selectInput() | Dropdown menu |
| textInput() | Free text box |
| checkboxInput() | Single checkbox |
| dateRangeInput() | Date range picker |
4. Reactive Expressions
Use reactive() to cache and reuse computed values across multiple outputs, avoiding redundant calculations:
server <- function(input, output) {
filtered_data <- reactive({
subset(df, department == input$dept)
})
output$table <- renderTable({ filtered_data() })
output$plot <- renderPlot({ hist(filtered_data()$salary) })
}
5. Plotly — Interactive Charts in R
plotly converts static ggplot2 charts into fully interactive ones (hover tooltips, zoom, pan) with a single function call:
install.packages("plotly")
library(plotly)
library(ggplot2)
p <- ggplot(df, aes(x = age, y = salary, color = department)) +
geom_point()
ggplotly(p) # instantly interactive!
6. Combining Shiny + Plotly
library(shiny)
library(plotly)
ui <- fluidPage(
plotlyOutput("interactivePlot")
)
server <- function(input, output) {
output$interactivePlot <- renderPlotly({
ggplotly(ggplot(df, aes(age, salary)) + geom_point())
})
}
shinyApp(ui, server)
7. Production-Ready Checklist
- ✅ Understand ui + server structure — the two building blocks of every Shiny app.
- ✅ Use reactive() to avoid redundant computation — for cleaner, faster apps.
- ✅ Use ggplotly() for instant interactivity — on top of existing ggplot2 charts.
- ✅ Test with runApp() — before considering deployment (covered in the final lesson).
Ready to master R Programming?
Build real-world data analysis and visualization projects with hands-on training, mentor-led sessions, and placement support.
.png)