Home > Blockchain >  How to create a shiny app with hierarchical filtering option
How to create a shiny app with hierarchical filtering option

Time:01-03

A subset of mydataset

mydata<-
        
structure(list(Country = c("belgium", "france", "brazil", "chile", 
"england", "germany", "france", "europe", "germany", "england", 
"germany", "colombia", "england", "argentina", "europe", "japan", 
"germany", "england", "europe", "spain", "europe", "spain", "spain", 
"europe", "netherlands", "spain", "belgium", "france", "france", 
"france", "germany", "france", "netherlands", "south", "germany", 
"belgium", "germany", "france", "spain", "spain", "europe", "france", 
"spain", "spain", "england", "italy", "brazil", "france", "england", 
"europe", "england", "paraguay", "argentina", "colombia", "europe", 
"colombia", "germany", "france", "argentina", "france", "uruguay", 
"europe", "europe", "belgium", "france", "europe", "argentina", 
"spain", "france", "france", "france", "germany", "europe", "france", 
"japan", "spain", "france", "germany", "netherlands", "argentina", 
"belgium", "japan", "england", "england", "belgium", "uruguay", 
"england", "japan", "belgium", "england", "france", "italy", 
"germany", "england", "germany", "chile", "chile", "england", 
"portugal", "brazil", "europe", "england", "europe", "france", 
"colombia", "france", "italy", "italy", "south", "uruguay", "germany", 
"europe", "germany", "france", "belgium", "spain", "brazil", 
"netherlands", "italy", "france", "europe", "colombia", "france", 
"argentina", "colombia", "argentina", "portugal", "italy", "england", 
"colombia", "england", "england", "europe", "france", "china", 
"argentina", "germany", "belgium", "uruguay", "england", "portugal", 
"argentina", "italy", "france", "netherlands", "france", "chile", 
"belgium", "france", "france", "netherlands", "england", "argentina", 
"france", "france", "england", "italy", "france", "italy", "portugal", 
"europe", "brazil", "brazil", "argentina", "colombia", "spain", 
"spain", "south", "spain", "france", "germany", "france", "spain", 
"italy", "europe", "germany", "uruguay", "italy", "france", "france", 
"belgium", "spain", "france", "italy", "germany", "france", "colombia", 
"germany", "belgium", "argentina", "south", "netherlands", "germany", 
"germany", "colombia", "spain", "spain", "europe", "france", 
"south"), League = c("pro-league", "ligue-1", "serie-a", "primera-division", 
"premier-league", "bundesliga", "coupe-de-france", "uefa-womens-champions-league", 
"bundesliga", "championship", "2-bundesliga", "categoria-primera-a", 
"championship", "primera-division", "uefa-champions-league", 
"j1-league", "2-bundesliga", "premier-league", "uefa-champions-league", 
"segunda-division", "uefa-womens-champions-league", "segunda-division", 
"segunda-division", "uefa-womens-champions-league", "eredivisie", 
"segunda-division", "pro-league", "ligue-1", "ligue-1", "ligue-1", 
"2-bundesliga", "ligue-1", "eredivisie", "america-copa-libertadores", 
"2-bundesliga", "pro-league", "2-bundesliga", "ligue-1", "segunda-division", 
"la-liga", "uefa-womens-champions-league", "ligue-1", "segunda-division", 
"segunda-division", "championship", "serie-b", "serie-a", "coupe-de-france", 
"league-cup", "uefa-champions-league", "championship", "division-profesional", 
"primera-division", "categoria-primera-a", "uefa-womens-champions-league", 
"categoria-primera-a", "bundesliga", "ligue-1", "primera-division", 
"ligue-2", "primera-division", "uefa-champions-league", "uefa-champions-league", 
"pro-league", "ligue-2", "uefa-womens-champions-league", "primera-division", 
"segunda-division", "ligue-1", "ligue-1", "ligue-2", "bundesliga", 
"uefa-womens-champions-league", "ligue-1", "j1-league", "la-liga", 
"ligue-1", "2-bundesliga", "eredivisie", "primera-division", 
"pro-league", "j1-league", "championship", "championship", "pro-league", 
"primera-division", "championship", "j1-league", "pro-league", 
"premier-league", "ligue-1", "serie-a", "bundesliga", "championship", 
"bundesliga", "primera-division", "primera-division", "championship", 
"liga-nos", "serie-a", "uefa-champions-league", "championship", 
"uefa-champions-league", "coupe-de-france", "categoria-primera-a", 
"ligue-1", "coppa-italia", "serie-b", "america-copa-libertadores", 
"primera-division", "bundesliga", "uefa-champions-league", "2-bundesliga", 
"ligue-2", "pro-league", "segunda-division", "serie-a", "eredivisie", 
"serie-a", "ligue-1", "uefa-champions-league", "categoria-primera-a", 
"ligue-1", "primera-division", "categoria-primera-a", "primera-division", 
"liga-nos", "serie-b", "championship", "categoria-primera-a", 
"premier-league", "championship", "uefa-womens-champions-league", 
"coupe-de-france", "chinese-super-league", "primera-division", 
"bundesliga", "pro-league", "primera-division", "championship", 
"liga-nos", "primera-division", "serie-b", "coupe-de-france", 
"eredivisie", "ligue-1", "primera-division", "pro-league", "ligue-1", 
"ligue-1", "eredivisie", "championship", "primera-division", 
"ligue-1", "ligue-1", "premier-league", "serie-b", "ligue-1", 
"serie-a", "liga-nos", "uefa-womens-champions-league", "serie-a", 
"serie-a", "primera-division", "categoria-primera-a", "segunda-division", 
"segunda-division", "america-copa-libertadores", "segunda-division", 
"ligue-1", "bundesliga", "ligue-1", "la-liga", "serie-b", "uefa-womens-champions-league", 
"bundesliga", "primera-division", "serie-a", "ligue-1", "coupe-de-france", 
"pro-league", "segunda-division", "ligue-1", "serie-a", "bundesliga", 
"ligue-1", "categoria-primera-a", "2-bundesliga", "pro-league", 
"primera-division", "america-copa-libertadores", "eredivisie", 
"2-bundesliga", "dfb-pokal", "categoria-primera-a", "segunda-division", 
"la-liga", "uefa-womens-champions-league", "ligue-2", "america-copa-libertadores"
), Year = c("2021", "2020", "2022", "2022", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2022", "2021", "2022", "2021", 
"2022", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2020", "2021", "2020", "2021", 
"2020", "2021", "2022", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2022", 
"2021", "2021", "2021", "2021", "2022", "2022", "2022", "2021", 
"2022", "2021", "2021", "2022", "2021", "2022", "2021", "2021", 
"2021", "2021", "2021", "2022", "2021", "2020", "2021", "2021", 
"2021", "2021", "2020", "2022", "2021", "2021", "2021", "2021", 
"2022", "2021", "2022", "2021", "2021", "2021", "2022", "2021", 
"2022", "2021", "2021", "2020", "2021", "2021", "2021", "2021", 
"2022", "2022", "2021", "2021", "2022", "2021", "2021", "2021", 
"2021", "2022", "2021", "2021", "2021", "2022", "2022", "2021", 
"2021", "2021", "2021", "2021", "2021", "2022", "2021", "2021", 
"2021", "2021", "2022", "2021", "2022", "2022", "2022", "2021", 
"2021", "2021", "2022", "2021", "2021", "2021", "2021", "2022", 
"2022", "2021", "2021", "2022", "2021", "2021", "2022", "2021", 
"2021", "2021", "2021", "2022", "2021", "2021", "2020", "2021", 
"2021", "2022", "2021", "2020", "2021", "2021", "2021", "2021", 
"2021", "2021", "2022", "2022", "2022", "2022", "2021", "2021", 
"2022", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2022", "2021", "2020", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2022", "2021", "2021", "2022", "2022", 
"2021", "2021", "2021", "2022", "2021", "2021", "2021", "2021", 
"2022"), Club = c("Sint-Truiden", "Reims", "Coritiba", "Ñublense", 
"Chelsea", "Eintracht Frankfurt", "Chusclan-Laudun-l'Ardois", 
"Skiljebo U19", "Stuttgart", "Bristol City", "Hansa Rostock", 
"Envigado", "Reading", "Arsenal de Sarandí", "Real Madrid", "Sanfrecce Hiroshima", 
"Heidenheim", "Everton", "RB Leipzig", "Almería", "Chelsea Women", 
"Burgos CF", "CD Lugo", "Malmö FF U19", "Heerenveen", "Leganés", 
"RSC Anderlecht", "Rennes", "Reims", "Montpellier", "Paderborn", 
"Brest", "Heracles", "Atlético Mineiro", "Dynamo Dresden", "Sporting Charleroi", 
"Nürnberg", "Olympique Marseille", "Burgos CF", "Deportivo Alavés", 
"Öster U19", "Olympique Marseille", "SD Ponferradina", "Real Zaragoza", 
"Peterborough United", "SPAL", "Juventude", "Illzach Modenheim", 
"Mansfield Town", "Villarreal", "West Bromwich Albion", "Olimpia", 
"Boca Juniors", "Deportivo Pereira", "Brommapojkarna U19", "Unión Magdalena", 
"Bayern München", "Montpellier", "Central Córdoba SdE", "Caen", 
"Nacional", "Dinamo Tbilisi", "Inter Milan", "Sint-Truiden", 
"Caen", "Landskrona BoIS U19", "Defensa y Justicia", "SD Ponferradina", 
"Rennes", "Strasbourg", "Valenciennes", "Bochum", "Halmstad U19", 
"Lorient", "Yokohama F. Marinos", "Real Sociedad", "Montpellier", 
"Heidenheim", "Heracles", "Racing Club", "Royal Antwerp FC", 
"Nagoya Grampus", "Blackburn Rovers", "Millwall", "Sint-Truiden", 
"Fénix", "Peterborough United", "Shimizu S-Pulse", "Sporting Charleroi", 
"Liverpool", "Lorient", "AC Milan", "Borussia M'gladbach", "Blackburn Rovers", 
"RB Leipzig", "Curicó Unido", "Universidad Católica", "Cardiff City", 
"Benfica", "Fluminense", "Club Brugge", "Bristol City", "Ajax", 
"Créteil", "Millonarios", "Lorient", "Venezia", "Lecce", "Talleres Córdoba", 
"Fénix", "Eintracht Frankfurt", "Atalanta", "Heidenheim", "Grenoble Foot 38", 
"RFC Seraing", "SD Amorebieta", "Ceará", "NEC", "Cagliari", "Lille", 
"Ferencváros", "Deportivo Pereira", "Saint-Étienne", "Racing Club", 
"Deportes Tolima", "Boca Juniors", "Paços de Ferreira", "Perugia", 
"Coventry City", "Jaguares de Córdoba", "Aston Villa", "Swansea City", 
"GAIS U19", "Schiltigheim", "Hebei CFFC", "Lanús", "Bayer Leverkusen", 
"Royal Antwerp FC", "Albion", "Hull City", "FC Arouca", "Platense", 
"Pisa", "Red Star", "Ajax", "Lille", "O'Higgins", "Club Brugge", 
"Lens", "Olympique Marseille", "Go Ahead Eagles", "Birmingham City", 
"Central Córdoba SdE", "Clermont", "Metz", "Wolverhampton Wanderers", 
"Brescia", "Troyes", "Cagliari", "Sporting Braga", "Hammarby U19", 
"Fortaleza", "Coritiba", "Sarmiento", "Once Caldas", "AD Alcorcón", 
"CD Tenerife", "Flamengo", "Mirandés", "Olympique Lyonnais", 
"Hertha BSC", "Lens", "Deportivo Alavés", "Parma", "Kalmar U19", 
"Augsburg", "Torque", "Sassuolo", "Bordeaux", "Molsheim Ernolsheim", 
"Union Saint-Gilloise", "Real Zaragoza", "Saint-Étienne", "Cagliari", 
"Eintracht Frankfurt", "Saint-Étienne", "Millonarios", "Ingolstadt", 
"KV Mechelen", "Banfield", "Libertad", "Fortuna Sittard", "Holstein Kiel", 
"Stuttgart", "La Equidad", "SD Amorebieta", "Rayo Vallecano", 
"Syrianska U19", "Pau", "Caracas"), total_goal_count = c(4L, 
0L, 4L, 2L, 7L, 2L, 3L, 7L, 5L, 5L, 3L, 0L, 2L, 2L, 4L, 3L, 0L, 
5L, 3L, 0L, 1L, 4L, 1L, 1L, 4L, 1L, 2L, 3L, 2L, 3L, 3L, 1L, 2L, 
1L, 3L, 1L, 4L, 4L, 3L, 5L, 1L, 2L, 3L, 2L, 4L, 2L, 3L, 2L, 3L, 
2L, 3L, 5L, 1L, 2L, 4L, 0L, 4L, 2L, 6L, 1L, 2L, 3L, 2L, 3L, 4L, 
1L, 3L, 5L, 1L, 2L, 1L, 4L, 1L, 3L, 4L, 1L, 0L, 2L, 2L, 2L, 4L, 
1L, 0L, 1L, 3L, 1L, 4L, 2L, 4L, 1L, 3L, 0L, 2L, 1L, 4L, 4L, 3L, 
3L, 2L, 4L, 6L, 5L, 1L, 3L, 1L, 0L, 4L, 4L, 1L, 1L, 2L, 5L, 3L, 
2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 3L, 5L, 5L, 2L, 8L, 2L, 1L, 1L, 
3L, 2L, 0L, 4L, 2L, 3L, 1L, 3L, 4L, 4L, 4L, 6L, 2L, 3L, 2L, 3L, 
2L, 2L, 0L, 2L, 4L, 5L, 3L, 4L, 4L, 3L, 3L, 2L, 2L, 4L, 1L, 2L, 
2L, 1L, 3L, 4L, 1L, 0L, 3L, 2L, 6L, 1L, 1L, 3L, 2L, 4L, 2L, 2L, 
4L, 3L, 2L, 1L, 2L, 2L, 2L, 3L, 4L, 0L, 2L, 3L, 2L, 5L, 4L, 3L, 
2L, 2L, 1L, 2L, 2L, 5L, 0L)), row.names = c(NA, -200L), class = c("tbl_df", 
"tbl", "data.frame"))

My shiny architecture:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  
  dashboardHeader(title = "FOOTBAL ANALYTICS",titleWidth = 1000),
  dashboardSidebar(
    
    sidebarMenu(
      
      selectInput("Country_id","Country", choices = LETTERS),
      selectInput("League_id","league",choices = LETTERS),
      selectInput("Club_id","Club",choices = LETTERS),
      
      menuItem("Offensive statistics",
               menuSubItem("Menu 1.1"),
               menuSubItem("Menu 1.2")
        
      ),
      
      menuItem("Defensive statistics",
               menuSubItem("Menu 2.1"),
               menuSubItem("Menu 2.2")
        
      ),
      
      menuItem("Overall statistics",
               menuSubItem("Menu 3.1"),
               menuSubItem("Menu 3.2")
        
      )
      
      
    )
  ),
  dashboardBody()
  
  
  
)





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

shinyApp(ui, server)

My goal is to filter hierarchically and optimally so that my "shiny" runs faster. My database is very large, almost a million lines, if not more.

The objective is to filter on the country first, once the country is filtered, the filtering cell on the league will display only the leagues of this country, where I can decide to select one of the leagues of the country and once this league is chosen, I can select only the clubs of this league and filter only on the data of this club. So we have a filtering hierarchy Country-League-Club.

For example I want to filter on france then league-1 then Montpellier

Normally if I to decide filter on france, the league: bundesliga should not be displayed in the cell of filtering on league.

Translated with www.DeepL.com/Translator (free version)

CodePudding user response:

You can combination of renderUI and uiOutput to do this dynamically on the server side.

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "FOOTBAL ANALYTICS",titleWidth = 1000),
  dashboardSidebar(
    sidebarMenu(
      uiOutput('dd1'), 
      uiOutput('dd2'), 
      uiOutput('dd3'), 
      menuItem("Offensive statistics",
               menuSubItem("Menu 1.1"),
               menuSubItem("Menu 1.2")
      ),
      menuItem("Defensive statistics",
               menuSubItem("Menu 2.1"),
               menuSubItem("Menu 2.2")
      ),
      menuItem("Overall statistics",
               menuSubItem("Menu 3.1"),
               menuSubItem("Menu 3.2")
      )
    )
  ),
  dashboardBody(
    tableOutput("data")
  )
)

server <- function(input, output, session) {
  output$dd1 <- renderUI({
    selectInput("Country_id","Country", choices = mydata$Country) 
  })
  
  output$dd2 <- renderUI({
    req(input$Country_id)
    selectInput("League_id","league",choices = mydata$League[mydata$Country == input$Country_id])
  })
  
  output$dd3 <- renderUI({
    req(input$League_id)
    selectInput("Club_id","Club",choices = mydata$Club[mydata$Country == input$Country_id & mydata$League == input$League_id])
  })
  
  output$data <- renderTable({
    req(input$Club_id)
    subset(mydata, Country == input$Country_id & League == input$League_id & Club == input$Club_id)
  })

}

shinyApp(ui, server)

enter image description here

  • Related