Barplot in R shiny

Hii. I am making a dashboard for monitoring surveys. I want to capture the number of surveys done by enumerators. In my dashboard, I want all the enumerators to be shown together. Like if I have 5 enumerators and I click the "Run App", by default all the enumerators bars come together. But I must also be able to look at an individual enumerator performance as well. Example, I want only the enumerator X 's bar to be shown. When I click that name on sidebar only that bar must be visible. Is this possible?

library(tidyverse)
library(shiny)
library(shinydashboard)
#> 
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#> 
#>     box
data<-tibble::tribble(
  ~enumerator,
     "gandhi",
      "nehru",
      "patel",
     "rajaji",
     "rajaji",
     "rajaji",
     "rajaji",
     "gandhi",
     "gandhi",
     "gandhi",
     "gandhi",
      "nehru",
      "nehru",
      "nehru",
      "nehru",
      "nehru",
      "nehru",
      "patel",
      "patel",
      "patel",
      "patel"
  )

ui<-dashboardPage(
  skin="red",
  dashboardHeader(title = "count of surveys"),
  dashboardSidebar("Select inputs here",
                   selectInput("enum","Select enumerator",choices = unique(data$enumerator))),
  dashboardBody(
    tabsetPanel(type="tabs",
                id="tab_selected",
                tabPanel(title="first view",
                  plotOutput("plot1")
                )
)))

server<-function(input,output){
  output$plot1<-renderPlot({
    ggplot(aes(enumerator,fill=enumerator))+
    geom_bar(width=0.5)+
    theme_classic()}
  )
}

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-04-08 by the reprex package (v2.0.1)

Here is a sketch of one solutions.

library(tidyverse)
library(shiny)
library(shinydashboard)
#> 
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#> 
#>     box
data<-tibble::tribble(
  ~enumerator,
  "gandhi",
  "nehru",
  "patel",
  "rajaji",
  "rajaji",
  "rajaji",
  "rajaji",
  "gandhi",
  "gandhi",
  "gandhi",
  "gandhi",
  "nehru",
  "nehru",
  "nehru",
  "nehru",
  "nehru",
  "nehru",
  "patel",
  "patel",
  "patel",
  "patel"
)

ui<-dashboardPage(
  skin="red",
  dashboardHeader(title = "count of surveys"),
  dashboardSidebar("Select inputs here",
                   selectInput("enum","Select enumerator",
                               choices = c("All", unique(data$enumerator)))),
  dashboardBody(
    tabsetPanel(type="tabs",
                id="tab_selected",
                tabPanel(title="first view",
                         plotOutput("plot1")
                )
    )))

server<-function(input,output){
  FilteredData <- reactive({
    if (input$enum == "All") {
      data
    } else {
    data[data$enumerator == input$enum, ]
    }
  })
  output$plot1<-renderPlot({
    ggplot(FilteredData(),aes(enumerator,fill=enumerator))+
      geom_bar(width=0.5)+
      theme_classic()}
  )
}

shinyApp(ui,server)

You solution look correct. But when I implemented in a dashboard I did for iris dataset, it does not work. The code is below. Am i implementing it wrong?

library(tidyverse)
library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
library(extrafont)
#> Warning: package 'extrafont' was built under R version 4.1.2
#> Registering fonts with R
library(shiny)
library(shinydashboard)
#> 
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#> 
#>     box
library(gridExtra)
#> Warning: package 'gridExtra' was built under R version 4.1.3
#> 
#> Attaching package: 'gridExtra'
#> The following object is masked from 'package:dplyr':
#> 
#>     combine
windowsFonts(a=windowsFont("Times New Roman"))


ui<-dashboardPage(
  skin="red",
  dashboardHeader(title = "Iris dataset dashboard"),
  dashboardSidebar("Select the inputs here",
                   selectInput("species","Select the species",choices = c("All",unique(iris$Species))
                               ,multiple = T)),
                   
  dashboardBody(
    tabsetPanel(
      type="tabs",
      id="tab_selected",
      tabPanel("First view",
               plotOutput("plot1",width = 1200,height = 900))
  )
))

server<-function(input,output){
  data_react<-reactive({ 
    
      filter(Species %in% input$species) %>% 
      group_by(Species) %>% 
      summarise(mean_petal_length=mean(Petal.Length),
                mean_petal_width=mean(Petal.Width),
                mean_sepal_length=mean(Sepal.Length),
                mean_sepal_width=mean(Sepal.Width))
      iris %>% 
      if(input$species=="All"){
        iris
      } else{
        iris[iris$Species==input$species, ]
      }
    
      })
  

  
  output$plot1<-renderPlot({
    g1<-ggplot(data_react(),aes(input$species,mean_petal_length))+
      geom_bar(stat="identity",width=0.5,fill="orange")+
      theme_classic()+
      geom_text(aes(label=mean_petal_length),size=6)+
      labs(title="Average value of Petal length of 3 species of iris",
           x="Species",
           y="Average value")+
      theme(plot.title = element_text(hjust=0.5,family = "a",face="bold",size = 18),
            text = element_text(family = "a"),
            axis.title = element_text(face="bold",size=15),
            axis.text = element_text(size=14))+
      scale_y_continuous(breaks = seq(0,10,by=1))
    
    g2<-ggplot(data_react(),aes(input$species,mean_petal_width))+
      geom_bar(stat="identity",width=0.5,fill="orange")+
      theme_classic()+
      geom_text(aes(label=mean_petal_width),size=6)+
      labs(title="Average value of Petal width of 3 species of iris",
           x="Species",
           y="Average value")+
      theme(plot.title = element_text(hjust=0.5,family = "a",face="bold",size = 18),
            text = element_text(family = "a"),
            axis.title = element_text(face="bold",size=15),
            axis.text = element_text(size=14))+
      scale_y_continuous(breaks = seq(0,3,by=0.5))
    
    g3<-ggplot(data_react(),aes(input$species,mean_sepal_length))+
      geom_bar(stat="identity",width=0.5,fill="orange")+
      theme_classic()+
      geom_text(aes(label=mean_sepal_length),size=6)+
      labs(title="Average value of Sepal length of 3 species of iris",
           x="Species",
           y="Average value")+
      theme(plot.title = element_text(hjust=0.5,family = "a",face="bold",size = 18),
            text = element_text(family = "a"),
            axis.title = element_text(face="bold",size=15),
            axis.text = element_text(size=14))+
      scale_y_continuous(breaks = seq(0,7,by=1))
   
     g4<-ggplot(data_react(),aes(input$species,mean_sepal_width))+
       geom_bar(stat="identity",width=0.5,fill="orange")+
       theme_classic()+
       geom_text(aes(label=mean_sepal_width),size=6)+
       labs(title="Average value of Sepal width of 3 species of iris",
            x="Species",
            y="Average value")+
       theme(plot.title = element_text(hjust=0.5,family = "a",face="bold",size = 18),
             text = element_text(family = "a"),
             axis.title = element_text(face="bold",size=15),
             axis.text = element_text(size=14))+
       scale_y_continuous(breaks = seq(0,4,by=0.5))
     
     
     grid.arrange(g1,g2,g3,g4,nrow=2, ncol=2)
  })
}


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-04-09 by the reprex package (v2.0.1)

I made substantial changes to the data_react() function and one change to the first plot. All of the plots need the same change.
In data_react(), I first filter the iris data and then summarize it. Your code tried to perform the filtering twice.
In the plot, you should refer to the columns in the data set with a bare column name, like Species, and not with the value in the column, which is what is stored in input$species.

server<-function(input,output){
  data_react<-reactive({ 
      if(input$species=="All"){
        tmp <- iris
      } else{
        tmp <- iris[iris$Species==input$species, ]
      }
    tmp %>% group_by(Species) %>% 
      summarise(mean_petal_length=mean(Petal.Length),
                mean_petal_width=mean(Petal.Width),
                mean_sepal_length=mean(Sepal.Length),
                mean_sepal_width=mean(Sepal.Width))
  })
  
  output$plot1<-renderPlot({
    g1<-ggplot(data_react(),aes(Species,mean_petal_length))+
      geom_bar(stat="identity",width=0.5,fill="orange")+
      theme_classic()+
      geom_text(aes(label=mean_petal_length),size=6)+
      labs(title="Average value of Petal length of 3 species of iris",
           x="Species",
           y="Average value")+
      theme(plot.title = element_text(hjust=0.5,family = "a",face="bold",size = 18),
            text = element_text(family = "a"),
            axis.title = element_text(face="bold",size=15),
            axis.text = element_text(size=14))+
      scale_y_continuous(breaks = seq(0,10,by=1))

Ohh I see. Thanks a lot for the help. I am still learning Shiny, so make a lot of errors.

Regards,
Nithin

This topic was automatically closed 7 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.