【发布时间】:2020-05-13 14:47:11
【问题描述】:
我是 r 的新手,我正在学习构建闪亮的基本应用程序,我的文件 server.R 中的 if else 条件存在问题。我尝试设置if else 条件,但没有成功。
这是我的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"))
)
)
)
)
)
)
这是我的if-elseserver.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")
}
}
}
我被卡住了,因为我不知道要设置什么条件。
对此的任何帮助将不胜感激!谢谢!!
【问题讨论】:
标签: r if-statement nested-loops