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