0

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!!

Ben10
  • 287
  • 1
  • 10

1 Answers1

1

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)
Ben
  • 28,684
  • 5
  • 23
  • 45
  • I might confuse with your answer can you check my ```ui.R``` and ```server.R``` for ```if-else``` statement thanks in advance. I updated it – Ben10 May 13 '20 at 16:49
  • in case you need css, html, and csv file: https://gitlab.com/Schrodinger168/practice/-/tree/master – Ben10 May 13 '20 at 16:56
  • Okay I'll check now – Ben10 May 13 '20 at 17:05
  • Can you explain me what is ```sel_continent``` here? – Ben10 May 13 '20 at 17:09
  • I see it just a variable you represented – Ben10 May 13 '20 at 17:11
  • I made a variable `sel_continent` that is the English translation of the continent. The `case_when` will compare with each (presumed) French continent name and have `sel_continent` contain the English name. It looks like your data may use the English names. In the `filter` statement, you can compare the `continent_level` in your `cv_continent` data frame with `sel_continent`. Does that help? – Ben May 13 '20 at 17:14
  • What is ```!!sym(..)``` mean? Can you explain a bit? – Ben10 May 13 '20 at 17:24
  • `sel_condition` is a character string of "deaths" or "cases". If you compare with what you had before (deaths or cases) - you'll notice your version had these column names that weren't in quotes (because they were symbols). There's 2 things you need to do. One is make symbol from the character string. The second is to unquote the result in the `dplyr` pipe. You can read more about this [here](https://stackoverflow.com/questions/27197617/filter-data-frame-by-character-column-name-in-dplyr) or [here](https://dplyr.tidyverse.org/articles/programming.html#unquoting). – Ben May 13 '20 at 17:40
  • Okay thank and for symbol like ```!!sym(..)``` what is the goal of using it? – Ben10 May 13 '20 at 17:46
  • 1
    That takes the string "cases" and converts to symbol with the same name (cases), so it will be understood as the column name within `cv_continent`, and not as a character string. – Ben May 13 '20 at 17:47
  • So it is a predefined function ```!!sym()``` like that or it is just ```sym()``` and we added symbol ```!!``` before it – Ben10 May 13 '20 at 17:51
  • 1
    It's 2 functions. `sym` is one function. The bang-bang `!!` is syntactic sugar to unquote. See reference: https://dplyr.tidyverse.org/articles/programming.html#unquoting – Ben May 13 '20 at 17:52