How to make dynamic graphs in Shiny?

Hello everyone!! I am beginner in Shiny. I began with creating simple dashboard of gapminder dataset. But I am not able to make a graph that shows the gains in life expectancy over the years as per the change in the input continent.

library(tidyverse)
library(shiny)
library(shinydashboard)
#> 
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#> 
#>     box
library(shinyjs)
#> Warning: package 'shinyjs' was built under R version 4.1.2
#> 
#> Attaching package: 'shinyjs'
#> The following object is masked from 'package:shiny':
#> 
#>     runExample
#> The following objects are masked from 'package:methods':
#> 
#>     removeClass, show
library(ggthemes)
library(gapminder)
library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
library(plotly)
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
gapminder<-gapminder %>% clean_names()



ui<-dashboardPage(
    skin="red",
    
    dashboardHeader(title="Has our world become more prosperous?",titleWidth = 500),
    
    dashboardSidebar(
        width = 250,
        br(),
        h4("Select the inputs here"),
        selectInput("continent","Select the continent",choices=sort(unique(continent$continent))),
        selectInput("country","Select the countries",choices = sort(unique(gapminder$country)),multiple=TRUE)),
        
        dashboardBody(
        tabsetPanel(
            type="tabs",
            id="tab_selecetd",
            tabPanel(title="Continent-view",
            plotOutput("plot2")
        )
    )

    
    ))
#> Error in unique(continent$continent): object 'continent' not found
server<-function(input,output){
    
    observe(print(input$continent))
   
   continent<-reactive({
       gapminder %>% 
        group_by(input$continent,year) %>% 
        summarise(mean_exp=mean(life_exp))
   })
    
    output$plot2<-renderPlot({
        
        ggplot(continent(),aes(year,mean_exp))+
            geom_line(size=1.2)+theme_minimal()+
            labs(title="Gains in life expectancy continent-wise")+
            xlab("Year")+
            ylab("Average Life Expectancy")
            })
    
    
}

shinyApp(ui,server)
#> Error in force(ui): object 'ui' not found

Created on 2022-02-04 by the reprex package (v2.0.1)

Hi there,

Here is a slimmed down version of what you are asking for (only minimum amount of packages and code)

library(tidyverse)
library(shiny)
library(shinydashboard)
library(gapminder)


ui<-dashboardPage(
  skin="red",
  
  dashboardHeader(title="Has our world become more prosperous?",titleWidth = 500),
  
  dashboardSidebar(
    width = 250,
    br(),
    h4("Select the inputs here"),
    selectInput("continent","Select the continent",
                choices=sort(unique(gapminder$continent)))),
  
  dashboardBody(
    tabsetPanel(
      type="tabs",
      id="tab_selecetd",
      tabPanel(title="Continent-view",
               plotOutput("plot2")
      )
    )
    
  ))

server<-function(input,output){
  
  continent<-reactive({
    gapminder %>% 
      filter(continent == input$continent) %>% 
      group_by(year) %>% 
      summarise(mean_exp=mean(lifeExp))
  })
  
  output$plot2<-renderPlot({
    ggplot(continent(),aes(year,mean_exp))+
      geom_line(size=1.2)+theme_minimal()+
      labs(title="Gains in life expectancy continent-wise")+
      xlab("Year")+
      ylab("Average Life Expectancy")
  })
  
  
}

shinyApp(ui,server)

The most important change I made was when defining the continent variable you need to filter by continent, not group :slight_smile:

In the code was also a placeholder for Country, and you could add a filter for that, but it will clash with the Continent one if you don't put in extra (Shiny) code as countries from different continents would cause issues in display (if you want to use the sample plot).

Hope this helps,
PJ

Thanks a lot for this. My next step in fact was providing the relevant countries for a particular continent selected. Will have to check how to do this.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.