Introduction to Shiny for Interactive Dashboards
Join our community on Telegram!
Join the biggest community of Pharma students and professionals.
Shiny is an R package that allows users to build interactive web applications and dashboards directly from R. It enables analysts to create dynamic visualizations, data exploration tools, and interactive reports without needing advanced web development skills.
With Shiny, users can build applications where inputs such as dropdown menus, sliders, and buttons control the output in real time. This makes it easier to explore data, test scenarios, and present results in an interactive format.
To use Shiny, the package must first be installed and loaded into the R session.
install.packages("shiny")
library(shiny)
A basic Shiny application consists of two main parts: the user interface and the server.
| Component | Purpose |
|---|---|
| User Interface (UI) | Defines the layout and input/output elements of the app |
| Server | Contains the logic that processes inputs and generates outputs |
Below is a simple example of a Shiny application that displays a histogram with a user-controlled number of bins.
library(shiny)
ui <- fluidPage(
titlePanel("Simple Shiny App"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 10)
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$eruptions
hist(x, breaks = input$bins,
col = "skyblue",
border = "white")
})
}
shinyApp(ui = ui, server = server)
When this code is run, it launches an interactive web application where the user can adjust the slider to change the number of bins in the histogram.
Shiny is widely used for building dashboards, data exploration tools, and interactive reports. It allows analysts to present data in a more engaging and user-friendly way, making it easier for stakeholders to interact with and understand the results.
