Hi. I am a beginner to R shiny. As practice, I am working on gapminder dataset. I have 2 filters. The first is of continent and second of countries. Once I select a continent, only the relevant countries must appear in the next filter. How can I do this?
library(tidyverse)
library(shiny)
library(shinydashboard)
#>
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#>
#> box
library(janitor)
#>
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#>
#> chisq.test, fisher.test
library(gapminder)
library(ggthemes)
gapminder<-gapminder %>% clean_names()
ui<-dashboardPage(
skin="red",
dashboardHeader(title="Life expectancy across continents",titleWidth = 500),
dashboardSidebar(selectInput("continent","Select the continent",choices = sort(unique(gapminder$continent)),multiple=T,selected="Asia"),
selectInput("country","Select the country",choices=unique(gapminder$country),multiple=T)),
dashboardBody(
tabsetPanel(
type="tabs",
id="tab_selected",
tabPanel(title="Continent-view",
plotOutput("plot1"))
)
)
)
server<-function(input,output){
continent<-reactive({
gapminder %>%
filter(continent %in% input$continent) %>%
group_by(continent,year) %>%
summarise(mean_exp=mean(life_exp),.groups = "drop")
})
output$plot1<-renderPlot({
ggplot(continent(),aes(year,mean_exp,color=continent))+
geom_line(size=1.5)+
theme_minimal()+
labs(title="How has life expectancy increased across different continents in the post-war period?",x="Year",y="Average Life Expectancy")
})
}
shinyApp(ui,server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents
Created on 2022-03-07 by the reprex package (v2.0.1)