Formation R

Shiny: Introduction

Qu’est-ce que Shiny?

  • Un Package R
  •       Une Application web
  •             Basée essentiellement sur du R

Installation du package

install.packages('shiny')
library(shiny)

Shiny App Structure

library(shiny)


ui <- fluidPage()

server <- function(input, output, session){}

shinyApp(ui, server)

ui & server

  • UI: Ce qui apparait à l’écran
  • Server: Calculs effectués derrière

ui & server

  • UI: Ce qui apparait à l’écran
  • Server: Calculs effectués derrière

Application simple

App : UI & Server

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

UI

ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),
    
    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

Server

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

3 Types de fonctions

  • input : sliderInput()
  • output : plotOutput()
  • layout : fluidPage(), sidebarLayout(), …

input function

  • Nom terminant par Input
  • Structure commune : fctInput( 'id' , 'label', ...)

output function

  • textOutput('id'), verbatimTextOutput('id')
  • tableOutput('id'), dataTableOutput('id')
  • plotOutput('id')

output function

  • textOutput('id'), verbatimTextOutput('id')
  • tableOutput('id'), dataTableOutput('id')
  • plotOutput('id')

output function

  • textOutput('id'), verbatimTextOutput('id')
  • tableOutput('id'), dataTableOutput('id')
  • plotOutput('id')

output function

  • textOutput('id'), verbatimTextOutput('id')
  • tableOutput('id'), dataTableOutput('id')
  • plotOutput('id')

output function

  • textOutput('id'), verbatimTextOutput('id')
  • tableOutput('id'), dataTableOutput('id')
  • plotOutput('id')

output function

  • textOutput('id'), verbatimTextOutput('id')
  • tableOutput('id'), dataTableOutput('id')
  • plotOutput('id')

layout function

layout function

layout function

layout function

layout function

layout function

layout function

Server

server <- function(input, output) {

    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}
  • distplot est l’id qui sera dans plotOutput()
  • Dans l’UI : plotOutput('displot' )

Render function

  • Commence par render
  • Mettent à jour l’ouput associé

Mecanisme

Mecanisme

Mecanisme

Mecanisme

Mecanisme

Mecanisme

Mecanisme

Mecanisme

Mecanisme

Mecanisme

Mecanisme

Récapitulatif

Fondamentaux

  • 2 parties : Server et UI (interface)
  • UI : input, output & layout function
  • Server : renderFct est associée à un output

Instructions

  • Lire le fichier df_unemp.rds
  • Créer une App avec un tableau reactable
  • Ajouter des selectInput()
  • Ameliorer le code

Questions?