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

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

12 Comments

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
in case you need css, html, and csv file: gitlab.com/Schrodinger168/practice/-/tree/master
Okay I'll check now
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.
It's 2 functions. sym is one function. The bang-bang !! is syntactic sugar to unquote. See reference: dplyr.tidyverse.org/articles/programming.html#unquoting
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.