library(shiny)
## Warning: package 'shiny' was built under R version 4.3.3
library(shinydashboard)
##
## Attaching package: 'shinydashboard'
## The following object is masked from 'package:graphics':
##
## box
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(DT)
##
## Attaching package: 'DT'
## The following objects are masked from 'package:shiny':
##
## dataTableOutput, renderDataTable
ui <- dashboardPage(
dashboardHeader(title = "Stress Monitoring Dashboard"),
dashboardSidebar(
sidebarMenu(
menuItem("Real-time Data", tabName = "realtime", icon = icon("dashboard")),
menuItem("Stress Analysis", tabName = "analysis", icon = icon("chart-line")),
menuItem("Recommendations", tabName = "recommendations", icon = icon("lightbulb"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "realtime",
fluidRow(
box(plotlyOutput("stressPlot"), width = 8),
box(
h4("Stress Level:"),
verbatimTextOutput("stressLevel"),
width = 4
)
)),
tabItem(tabName = "analysis",
fluidRow(
box(DTOutput("stressTable"), width = 12)
)),
tabItem(tabName = "recommendations",
fluidRow(
box(
h4("Recommendations:"),
verbatimTextOutput("recommendations"),
width = 12
)
))
)
)
)
server <- function(input, output, session) {
# Simulate real-time data
stressData <- reactiveVal(data.frame(Time = Sys.time(), Stress = runif(1, 0, 100)))
reactiveTimer <- reactiveTimer(1000) # Update every 1 second
observe({
reactiveTimer()
new_data <- data.frame(Time = Sys.time(), Stress = runif(1, 0, 100))
stressData(rbind(stressData(), new_data))
})
# Real-time Plot
output$stressPlot <- renderPlotly({
plot_ly(
data = stressData(),
x = ~Time,
y = ~Stress,
type = "scatter",
mode = "lines+markers"
) %>% layout(title = "Real-time Stress Levels")
})
# Stress Level Display
output$stressLevel <- renderText({
latest_stress <- tail(stressData()$Stress, 1)
if (latest_stress > 70) {
paste("High Stress Detected:", round(latest_stress, 2))
} else {
paste("Stress Level Normal:", round(latest_stress, 2))
}
})
# Stress Table
output$stressTable <- renderDT({
datatable(stressData(), options = list(scrollX = TRUE))
})
# Recommendations (Mock for now)
output$recommendations <- renderText({
latest_stress <- tail(stressData()$Stress, 1)
if (latest_stress > 70) {
"Consider taking a short break and practicing deep breathing."
} else {
"Keep up the good work! Stay focused."
}
})
}
shinyApp(ui, server)
Shiny applications not supported in static R Markdown documents