mardi 12 mai 2020

How to set if condition in shiny r?

I am new to shiny and I have problem with the if condition.

Here is my ui.R:

library(shiny)
library(shinyWidgets)
library(shinydashboard)
library(shinythemes)
library(leaflet)
library(magrittr)
library(rvest)
library(readxl)
library(dplyr)
library(maps)
library(ggplot2)
library(reshape2)
library(plotly)

# import data
cv_global <- read.csv("coronavirus_global.csv")

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 du Covid-19 dans le monde dans la période de 4 mois\n")), style="color:#045a8d"),
                           span(tags$i(h2("Diagramme en barre")), style="color:#045a8d"),
                           selectInput("condition","Choisir observation:",
                                       choices = c("Cas","Nouveaux cas","Décès","Nouveaux décès")),
                        ),

                       plotOutput("image")

                      ),

             tabPanel("COVID-19 mapper",
                      div(class="outer",
                          tags$head(includeCSS("styles.css")),
                          leafletOutput("mymap", width="100%", height="100%"),
                      )

             )
  )

) 

Here is my server.R:

library(shiny)

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


  ConditionInput <- reactive({
    switch(input$condition,
           "Cas" = cases,
           "Nouveaux Cas" = new_cases,
           "Décès" = deaths,
           "Nouveaux décès" = new_deaths)
  })

#cas confirmés chaque mois
  cas <- cv_global %>%
    group_by(global_level, Month = format(as.Date(date), "%B")) %>%
    summarise(cases_each_month = sum(cases, na.rm = TRUE))

#nouveaux cas confirmés chaque mois   
  nouveaux_cas <- cv_global %>%
    group_by(global_level, Month = format(as.Date(date), "%B")) %>%
    summarise(new_cases_each_month = sum(new_cases, na.rm = TRUE))  

#décès chaque mois
  décès <- cv_global %>%
    group_by(global_level, Month = format(as.Date(date), "%B")) %>%
    summarise(deaths_each_month = sum(deaths, na.rm = TRUE))

#nouveaux décès chaque mois  
  nouveaux_décès <- cv_global %>%
    group_by(global_level, Month = format(as.Date(date), "%B")) %>%
    summarise(new_deaths_each_month = sum(new_deaths, na.rm = TRUE))


  output$image <- renderPlot({

    if(input$condition == "Cas") {
      cas %>%
        mutate(Month = factor(Month, levels = month.name)) %>%
        arrange(Month) %>%
        ggplot(aes(x = Month, y = cases_each_month )) +
        labs(title = "",x="Cas confirmés",y="Mois")+
        geom_col(fill= "khaki2")
    } 

    if(input$condition == "Nouveaux Cas"){
      nouveaux_cas %>%
        mutate(Month = factor(Month, levels = month.name)) %>%
        arrange(Month) %>%
        ggplot(aes(x = Month, y = new_cases_each_month )) +
        labs(title = "",x="Nouveaux Cas confirmés",y="Mois")+
        geom_col(fill= "khaki1")
    }

    if(input$condition == "Décès"){
      décès %>%
        mutate(Month = factor(Month, levels = month.name)) %>%
        arrange(Month) %>%
        ggplot(aes(x = Month, y = deaths_each_month )) +
        labs(title = "",x="Nouveaux Cas confirmés",y="Mois")+
        geom_col(fill= "firebrick")
    }

    if(input$condition == "Nouveaux Décès"){
      nouveaux_décès %>%
        mutate(Month = factor(Month, levels = month.name)) %>%
        arrange(Month) %>%
        ggplot(aes(x = Month, y = new_deaths_each_month )) +
        labs(title = "",x="Nouveaux Cas confirmés",y="Mois")+
        geom_col(fill= "firebrick1")
    }

  })



}

I have no idea since if I put only the first if condition it worked so well but when I began to put another if condition it stopped working I was stucked.

Anyway you can access the file like csv, html, css here: https://gitlab.com/Schrodinger168/practice/-/tree/master

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

Aucun commentaire:

Enregistrer un commentaire