Ben10
Ben10

Reputation: 297

Nested loop if in r

I am new to r and I am learning to build basic app of shiny and I have problems with if else condition in my file server.R. I tried to set the if else conditions but it did not work.

Here is my ui.R:

bootstrapPage(
  tags$head(includeHTML("gtag.html")),
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "VISUALISATION LE COVID-19", id="nav",

             tabPanel("GLOBAL",
                       sidebarPanel(
                           width= 20,
                           span(tags$i(h1("Visualiser la revolution globale du Covid-19\n")), style="color:#045a8d"),
                           span(tags$i(h2("Diagramme en barre")), style="color:#045a8d"),
                           selectInput("condition","Choisir observation:",
                                       choices = c("Cas","Décès","Nouveaux Cas","Nouveaux Décès"))
                        ),

                       plotOutput("image")

                      ),

             tabPanel("Continent",
                  sidebarLayout(
                      sidebarPanel(
                        span(tags$i(h4("Visualiser la revolution du Covid-19 par continent\n")), style="color:#045a8d"),
                        selectInput("continent","Choisir un continent:",
                                    choices = c("Afrique","Asie","Europe","Amérique du Nord","Océanie","Amérique du Sud")),
                        selectInput("condition","Choisir observation:",
                                    choices = c("Cas","Nouveaux Cas","Décès","Nouveaux Décès"))
                      ),

                      mainPanel(
                        tabsetPanel(
                          tabPanel("Diagramme en barre pour chaque continent", plotOutput("barre")),
                          tabPanel("Diagramme sectoriel", plotOutput("sectoriel"), width=15),
                          tabPanel("Dendrogramme", plotOutput("dendrogramme")),
                          tabPanel("Plan Factoriel",plotOutput("planfactoriel"))
                        )
                      )

                    )     
             )


  )

) 

Here is my if-else statement of server.R:

output$barre <- renderPlot({
if (input$continent == "Afrique"){

  if(input$condition == "Cas"){
    cv_continent %>% mutate(date = ymd(date)) %>%
    filter(continent_level %in% "Africa") %>%
    arrange(date) %>%
    transmute(date, Diff = c(0, diff(cases))) %>%
    ggplot(aes(x = date, y = Diff)) +
    labs(title = "Cas chaque jour",x="Mois",y="Cas confirmés")+
    geom_col(fill = "yellow") 
  }
  else if(input$condition == "Décès"){
    cv_continent %>% mutate(date = ymd(date)) %>%
      filter(continent_level %in% "Africa") %>%
      arrange(date) %>%
      transmute(date, Diff = c(0, diff(deaths))) %>%
      ggplot(aes(x = date, y = Diff)) +
      labs(title = "Décès chaque jour",x="Mois",y="Décès")+
      geom_col(fill = "red") 
  }
}

else if(input$continent == "Asie"){
  if(input$condition == "Cas"){
    cv_continent %>% mutate(date = ymd(date)) %>%
      filter(continent_level %in% "Asia") %>%
      arrange(date) %>%
      transmute(date, Diff = c(0, diff(cases))) %>%
      ggplot(aes(x = date, y = Diff)) +
      labs(title = "Cas chaque jour",x="Mois",y="Cas confirmés")+
      geom_col(fill = "yellow") 
  }
  else if(input$condition == "Décès"){
    cv_continent %>% mutate(date = ymd(date)) %>%
      filter(continent_level %in% "Asia") %>%
      arrange(date) %>%
      transmute(date, Diff = c(0, diff(deaths))) %>%
      ggplot(aes(x = date, y = Diff)) +
      labs(title = "Décès chaque jour",x="Mois",y="Décès")+
      geom_col(fill = "red") 
  } 

}

else if(input$continent == "Europe"){
  if (input$condition == "Cas"){
    cv_continent %>% mutate(date = ymd(date)) %>%
      filter(continent_level %in% "Europe") %>%
      arrange(date) %>%
      transmute(date, Diff = c(0, diff(cases))) %>%
      ggplot(aes(x = date, y = Diff)) +
      labs(title = "Cas chaque jour",x="Mois",y="Cas confirmés")+
      geom_col(fill = "yellow") 
  }
  else if(input$condition == "Décès"){
    cv_continent %>% mutate(date = ymd(date)) %>%
      filter(continent_level %in% "Europe") %>%
      arrange(date) %>%
      transmute(date, Diff = c(0, diff(deaths))) %>%
      ggplot(aes(x = date, y = Diff)) +
      labs(title = "Décès chaque jour",x="Mois",y="Décès")+
      geom_col(fill = "red") 
  }
}

else if(input$continent == "Amérique du Nord"){
  if(input$condition == "Cas"){
    cv_continent %>% mutate(date = ymd(date)) %>%
      filter(continent_level %in% "North America") %>%
      arrange(date) %>%
      transmute(date, Diff = c(0, diff(cases))) %>%
      ggplot(aes(x = date, y = Diff)) +
      labs(title = "Cas chaque jour",x="Mois",y="Cas confirmés")+
      geom_col(fill = "yellow")
  }
  else if(input$condition == "Décès"){
    cv_continent %>% mutate(date = ymd(date)) %>%
      filter(continent_level %in% "North America") %>%
      arrange(date) %>%
      transmute(date, Diff = c(0, diff(deaths))) %>%
      ggplot(aes(x = date, y = Diff)) +
      labs(title = "Décès chaque jour",x="Mois",y="Décès")+
      geom_col(fill = "red") 
}

}  
else if(input$continent == "Océanie"){
 if(input$condition == "Cas"){
   cv_continent %>% mutate(date = ymd(date)) %>%
     filter(continent_level %in% "Oceania") %>%
     arrange(date) %>%
     transmute(date, Diff = c(0, diff(cases))) %>%
     ggplot(aes(x = date, y = Diff)) +
     labs(title = "Cas chaque jour",x="Mois",y="Cas confirmés")+
     geom_col(fill = "yellow") 
 }
 else if(input$condition == "Décès"){ 
   cv_continent %>% mutate(date = ymd(date)) %>%
     filter(continent_level %in% "Oceania") %>%
     arrange(date) %>%
     transmute(date, Diff = c(0, diff(deaths))) %>%
     ggplot(aes(x = date, y = Diff)) +
     labs(title = "Décès chaque jour",x="Mois",y="Décès")+
     geom_col(fill = "red")  
  }  
}

else if(input$continent == "Amérique du Sud"){
  if(input$condition == "Cas"){
    cv_continent %>% mutate(date = ymd(date)) %>%
      filter(continent_level %in% "North America") %>%
      arrange(date) %>%
      transmute(date, Diff = c(0, diff(cases))) %>%
      ggplot(aes(x = date, y = Diff)) +
      labs(title = "Cas chaque jour",x="Mois",y="Cas confirmés")+
      geom_col(fill = "yellow") 
  }
  else if(input$condition == "Décès"){
    cv_continent %>% mutate(date = ymd(date)) %>%
      filter(continent_level %in% "North America") %>%
      arrange(date) %>%
      transmute(date, Diff = c(0, diff(deaths))) %>%
      ggplot(aes(x = date, y = Diff)) +
      labs(title = "Décès chaque jour",x="Mois",y="Décès")+
      geom_col(fill = "red") 
  }
}
}

I was stucked since I have no idea what to set the condition.

Any help for this would be much appreciated!! Thank you!!

Upvotes: 0

Views: 64

Answers (1)

Ben
Ben

Reputation: 30474

Maybe you want to "dry" up your code a bit, since your multiple plots look very similar.

Perhaps you want a single case_when statement to convert/translate the selectInput choice (e.g., from "Afrique" to "Africa"). I provided a few examples.

Then you might just need a couple of if statements for cases vs. deaths, and to select the fill color.

Edit: Upon review of ui, you have 2 selectInput with same id (they should be unique). I renamed them as condition_global and condition_continent. I also revised the variable names to match your ui.

library(shiny)
library(tidyverse)
library(ggplot2)
library(lubridate)
library(shinythemes)

ui <- bootstrapPage(
  #tags$head(includeHTML("gtag.html")),
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "VISUALISATION LE COVID-19", id="nav",

             tabPanel("GLOBAL",
                      sidebarPanel(
                        width= 20,
                        span(tags$i(h1("Visualiser la revolution globale du Covid-19\n")), style="color:#045a8d"),
                        span(tags$i(h2("Diagramme en barre")), style="color:#045a8d"),
                        selectInput("condition_global","Choisir observation:",
                                    choices = c("Cas","Décès","Nouveaux Cas","Nouveaux Décès"))
                      ),
                      plotOutput("image")
             ),

             tabPanel("Continent",
                      sidebarLayout(
                        sidebarPanel(
                          span(tags$i(h4("Visualiser la revolution du Covid-19 par continent\n")), style="color:#045a8d"),
                          selectInput("continent","Choisir un continent:",
                                      choices = c("Afrique","Asie","Europe","Amérique du Nord","Océanie","Amérique du Sud")),
                          selectInput("condition_continent","Choisir observation:",
                                      choices = c("Cas","Nouveaux Cas","Décès","Nouveaux Décès"))
                        ),

                        mainPanel(
                          tabsetPanel(
                            tabPanel("Diagramme en barre pour chaque continent", plotOutput("barre")),
                            tabPanel("Diagramme sectoriel", plotOutput("sectoriel"), width=15),
                            tabPanel("Dendrogramme", plotOutput("dendrogramme")),
                            tabPanel("Plan Factoriel",plotOutput("planfactoriel"))
                          )
                        )

                      )     
             )
  )
) 

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

  output$barre <- renderPlot({

    sel_continent <- case_when(
      input$continent == "Afrique" ~ "Africa",
      input$continent == "Asie" ~ "Asia",
      input$continent == "Europe" ~ "Europe",
      input$continent == "Amérique du Nord" ~ "North America",
      input$continent == "Océanie" ~ "Oceania",
      input$continent == "Amérique du Sud" ~ "South America"
    )

    if (input$condition_continent == "Cas") {
      sel_condition <- "cases"
      fill_color <- "yellow"
    }

    if (input$condition_continent == "Décès") {
      sel_condition <- "deaths"
      fill_color <- "red"
    }

    cv_continent %>% 
      mutate(date = ymd(date)) %>%
      filter(continent_level == sel_continent) %>%
      arrange(date) %>%
      transmute(date, Diff = c(0, diff(!!sym(sel_condition)))) %>%
      ggplot(aes(x = date, y = Diff)) +
        labs(title = paste(input$condition_continent, "chaque jour", x="Mois", y=input$condition_continent)) +
        geom_col(fill = fill_color) 

  })

}

shinyApp(ui, server)

Upvotes: 1

Related Questions