if (!require("pacman")) install.packages("pacman")
pacman::p_load(bslib, dplyr, ggplot2, here, readxl, shiny, shinylive, shinythemes, shinydashboard)
DF_path <- here("data", "DF_ERFI_anonym.xlsx")
DF <- read_excel(DF_path)
DF$OA_SATREP <- as.numeric(DF$OA_SATREP)
DF$MA_SEXE <- as.factor(DF$MA_SEXE)
DF2 <- subset(DF, select = c(OA_SATREP, MA_SEXE))
DF2 <- na.omit(DF2)
levels(DF2$MA_SEXE) <- c("Hommes", "Femmes")
# UI
ui <- fluidPage(
theme = shinytheme("flatly"),
titlePanel("Histogramme de la satisfaction de la répartition des tâches ménagères selon le genre"),
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "gender",
label = "Sélectionner le genre :",
choices = c("Tous" = "Tous", "Hommes" = "Hommes", "Femmes" = "Femmes"),
selected = "Tous"
)
),
mainPanel(
plotOutput("histPlot", height = "500px")
)
)
)
# Server
server <- function(input, output) {
output$histPlot <- renderPlot({
filtered_data <- DF2
if (input$gender != "Tous") {
filtered_data <- DF2 %>% filter(MA_SEXE == input$gender)
}
if (input$gender == "Tous") {
p <- ggplot(filtered_data, aes(x = OA_SATREP, fill = MA_SEXE)) +
geom_histogram(bins = 30, position = "dodge") +
facet_wrap(~ MA_SEXE, ncol = 2) +
labs(
title = "Histogramme de la satisfaction (Tous)",
x = "Satisfaction de la répartition des tâches ménagères",
y = "Fréquence"
) +
theme_minimal() +
scale_fill_manual(values = c("Hommes" = "aquamarine", "Femmes" = "steelblue"))
} else {
gender_color <- if (input$gender == "Hommes") "aquamarine" else "steelblue"
p <- ggplot(filtered_data, aes(x = OA_SATREP)) +
geom_histogram(bins = 30, fill = gender_color, color = "black") +
labs(
title = paste("Histogramme de la satisfaction (", input$gender, ")", sep = ""),
x = "Satisfaction de la répartition des tâches ménagères",
y = "Fréquence"
) +
theme_minimal() +
theme(legend.position = "none")
}
print(p)
})
}
# Run the app
shinyApp(ui = ui, server = server)Exemple avec données
Un exemple avec des données
Retrouvez cet exemple ici.