HAX815X - R programming
Aperçu des sections
-
-
Practical 1 Interactive visualisation with
mtcars
Objective Create a Shiny application to visualise the relationships between different variables in the
mtcars
dataset.Instructions
- Use
selectInput()
to allow the user to select two numerical variables from themtcars
dataset. - Display an interactive scatter graph (
ggplot2
). - Add a
sliderInput()
to adjust the size of the points.
library(ggplot2)
ui <- fluidPage(
titlePanel("Visualisation interactive des données mtcars"),
sidebarPanel(
selectInput("xvar", "Choisissez la variable X :", choices = names(mtcars)),
selectInput("yvar", "Choisissez la variable Y :", choices = names(mtcars)),
sliderInput("pointSize", "Taille des points :", min = 1, max = 10, value = 3)
),
mainPanel(
plotOutput("scatterPlot"), width = 20)
)
server <- function(input, output) {
output$scatterPlot <- renderPlot({
ggplot(mtcars, aes_string(x = input$xvar, y = input$yvar)) +
geom_point(size = input$pointSize, color = "blue") +
theme_minimal() +
labs(title = "Graphique de dispersion", x = input$xvar, y = input$yvar)
})
}
shinyApp(ui, server)Practical 2 Analysing
iris
dataObjective To enable the user to explore
iris
data using statistics and visualisations.Instructions
- Add a
selectInput()
to choose a species (setosa
,versicolor
,virginica
). - Filter the data according to the selected species.
- Display :
- A statistical summary (
summary()
) of the numerical variables. - A boxplot of the distribution of sepal lengths by species (
ggplot2
).
library(ggplot2)
ui <- fluidPage(
sidebarPanel(
selectInput("species", "Sélectionnez une espèce :", choices = unique(iris$Species))
),
mainPanel(
verbatimTextOutput("summaryStats"),
plotOutput("boxplotSepalLength")
)
)
server <- function(input, output) {
filteredData <- reactive({
iris[iris$Species==input$species,]
})
output$summaryStats <- renderPrint({
summary(filteredData()[, 1:4]) # Exclure la colonne Species
})
output$boxplotSepalLength <- renderPlot({
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Longueur des sépales par espèce")
})
}
shinyApp(ui, server)Practical 3 Analysis of the passengers on the Titanic (
titanic
from thedatasets
package)Objective To study the distribution of passengers according to class and survival.
Instructions
- Load the dataset with
data(‘Titanic’)
and convert it todata.frame
. - Add
checkboxGroupInput()
to allow the user to filter passengers by class and gender. - Display :
- A reactive array (
DT::datatable()
). - A bar chart of the number of survivors and non-survivors (
ggplot2
).
ui <- fluidPage(
titlePanel("Analyse des données Titanic"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("class", "Choisir la classe :", choices = unique(titanic_df$Class), selected = unique(titanic_df$Class)),
checkboxGroupInput("sex", "Choisir le sexe :", choices = unique(titanic_df$Sex), selected = unique(titanic_df$Sex))
),
mainPanel(
DTOutput("tableTitanic"),
plotOutput("barPlotSurvival")
)
)
)
server <- function(input, output) {
filteredData <- reactive({
titanic_df %>%
filter(Class %in% input$class, Sex %in% input$sex)
})
output$tableTitanic <- renderDT({
datatable(filteredData())
})
output$barPlotSurvival <- renderPlot({
ggplot(filteredData(), aes(x = Survived, y = Freq, fill = Survived)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Répartition des survivants et non-survivants", x = "Survie", y = "Nombre de passagers")
})
}
shinyApp(ui, server) - Use