Zainab Alhosni
2024-12-14
The shiny package in R is a framework for building interactive web applications directly in R. It allows users to create dashboards and interactive visualizations without requiring expertise in web development.
Key Features of Shiny:
Interactivity: Reactively updates outputs based on user input. Customizable: Supports HTML, CSS, and JavaScript for customization. Integration with R: Seamlessly integrates with R functions, including statistical modeling and visualization.
How It Works:
UI (User Interface): Defines the layout and elements displayed to users. Server: Contains the logic to process inputs and produce outputs. Shiny App: Combines ui and server components and runs as an application using shinyApp(ui, server).
Important Shiny Packages:
shiny: Core package for building applications. shinydashboard: Specialized for creating dashboards with predefined layouts. shinyWidgets: Adds interactive widgets like sliders and input fields. plotly or ggplotly: For interactive graphs. DT: For displaying and interacting with tabular data.
Uploading Data: Allow users to upload datasets. Descriptive Statistics: Summarize data using summary(), mean(), sd(), etc. Statistical Tests: Perform tests like t-tests, ANOVA, or regression analysis. Visualizations: Use ggplot2 or plotly to present results graphically. Code Example: Here’s an example of a Shiny app performing a basic t-test:
library(shiny)
ui <- fluidPage(
titlePanel("Statistical Analysis in Shiny"),
sidebarLayout(
sidebarPanel(
fileInput("file", "Upload CSV File", accept = ".csv"),
selectInput("var1", "Select Variable 1:", choices = NULL),
selectInput("var2", "Select Variable 2:", choices = NULL),
actionButton("analyze", "Run T-Test")
),
mainPanel(
verbatimTextOutput("testResult"),
plotOutput("boxPlot")
)
)
)
server <- function(input, output, session) {
data <- reactive({
req(input$file)
read.csv(input$file$datapath)
})
observe({
req(data())
updateSelectInput(session, "var1", choices = names(data()))
updateSelectInput(session, "var2", choices = names(data()))
})
result <- eventReactive(input$analyze, {
req(input$var1, input$var2)
t.test(data()[[input$var1]], data()[[input$var2]])
})
output$testResult <- renderPrint({
req(result())
result()
})
output$boxPlot <- renderPlot({
req(input$var1, input$var2)
boxplot(data()[[input$var1]], data()[[input$var2]],
names = c(input$var1, input$var2),
main = "Boxplot of Selected Variables")
})
}
shinyApp(ui, server)
##Demonstrate the Statistical Analysis as a Dashboard To enhance the application into a dashboard format, use the shinydashboard package.
Dashboard Features:
Sidebar: For navigation and inputs. Header and Body: For displaying analysis results and visualizations. Code Example Using shinydashboard:
##
## Attaching package: 'shinydashboard'
## The following object is masked from 'package:graphics':
##
## box
ui <- dashboardPage(
dashboardHeader(title = "Statistical Dashboard"),
dashboardSidebar(
fileInput("file", "Upload Data", accept = "(.csv"),
selectInput("var1", "Select Variable 1", choices = NULL),
selectInput("var2", "Select Variable 2", choices = NULL),
actionButton("analyze", "Run Analysis")
),
dashboardBody(
fluidRow(
box(title = "Statistical Test Results", status = "primary", solidHeader = TRUE,
verbatimTextOutput("testResult")),
box(title = "Visualization", status = "primary", solidHeader = TRUE,
plotOutput("boxPlot"))
)
)
)
server <- function(input, output, session) {
data <- reactive({
req(input$file)
read.csv(input$file$datapath)
})
observe({
req(data())
updateSelectInput(session, "var1", choices = names(data()))
updateSelectInput(session, "var2", choices = names(data()))
})
result <- eventReactive(input$analyze, {
req(input$var1, input$var2)
t.test(data()[[input$var1]], data()[[input$var2]])
})
output$testResult <- renderPrint({
req(result())
result()
})
output$boxPlot <- renderPlot({
req(input$var1, input$var2)
boxplot(data()[[input$var1]], data()[[input$var2]],
names = c(input$var1, input$var2),
main = "Boxplot of Selected Variables")
})
}
shinyApp(ui, server)