Hi,
I am making a dashboard of gapminder
dataset. I have given option "All" in continents and countries. But when the application opens, the names of continent does not appear; rather numbers (1,2,3,4,5) appear. Why does this happen?
How can I solve this issue?
library(tidyverse)
library(janitor)
library(gapminder)
library(plotly)
library(shiny)
library(shinydashboard)
windowsFonts(a=windowsFont("Times New Roman"))
gapminder<-gapminder %>%
clean_names()
ui<-dashboardPage(
skin = "red",
dashboardHeader(title = "Dashboard of Gapminder Dataset", titleWidth = 550),
dashboardSidebar("Select your inputs here",
selectInput("continent","Select the Continent",choices = c("All",unique(gapminder$continent)),multiple = T),
selectInput("country","Select the Country",choices = NULL,multiple = T),
selectInput("year","Select the Year",choices = c("All",unique(gapminder$year)),multiple=T)),
dashboardBody(
tabsetPanel(
tabPanel(title = "Life Expectancy Tab",
fluidRow(
column(6,plotlyOutput("plot1",height = 600,width = '100%')),
column(6,plotlyOutput("plot2",height = 600,width = '100%')),
column(6,plotlyOutput("plot3",height = 600,width = '100%')))
)
)))
server<-function(input,output,session){
observe({
x<-gapminder %>%
filter(continent %in% input$continent) %>%
select(country)
updateSelectInput(session,"country","Select the Country",choices = c("All",x))
})
#continent-wise life exp
cont_le<-reactive({
gapminder %>%
group_by(continent,year) %>%
summarise(avg_le=mean(life_exp)) %>%
filter(continent %in% input$continent)
})
#country-wise life exp
country_le<-reactive({
gapminder %>%
group_by(continent,country,year) %>%
summarise(mean_le=mean(life_exp)) %>%
filter(continent %in% input$continent,
country %in% input$country)
})
#life exp as of 2007 (latest year data)
latest_le<-reactive({
gapminder %>%
filter(year==2007) %>%
group_by(continent) %>%
summarise(avg_le=mean(life_exp)) %>%
filter(continent %in% input$continent)
})
output$plot1<-renderPlotly({
ggplot(cont_le(),aes(year,avg_le,group=continent,color=continent))+
geom_line(size=1.5)+
theme_minimal()+
labs(title = "Continent-wise Life Expectancy",
x="Year",
y="Average Life Expectancy")+
theme(plot.title = element_text(face="bold",hjust = .5,size = 18),
text = element_text(family = "a"),
axis.title = element_text(face = "bold",size=15))
})
output$plot2<-renderPlotly({
ggplot(country_le(),aes(year,mean_le,group=country,color=country))+
geom_line(size=1.5)+
theme_minimal()+
labs(title = "Country-wise Average Life Expectancy",
x="Year",
y="Average Life Expectancy")+
theme(plot.title = element_text(hjust=.5,size = 18,face='bold'),
text = element_text(family = "a"),
axis.title = element_text(face = "bold",size = 15))
})
output$plot3<-renderPlotly({
ggplot(latest_le(),aes(continent,avg_le,fill=continent))+
geom_col(width=0.6)+
theme_minimal()+
labs(title = "Average Life Expectancy of all continents as of 2007",
x="Continent",
y="Average Life Expectancy")+
theme(plot.title = element_text(hjust=.5,size = 18,face='bold'),
text = element_text(family = "a"),
axis.title = element_text(face = "bold",size = 15))
})
}
shinyApp(ui,server)