Search code examples
rshinydiagram

Diagram doesn't show in Shiny


I have a Shiny application that shows the relationship between race and number of employees in Silicon Valley companies. The toolbar on the left side is visible but the plot is not showing. How should I change my code?

Here is the code:

 library(shiny)
 library(ggplot2)
 library(dplyr)

 bcl <- read.csv("E:/country/data/reveal.csv")

 ui <- fluidPage(
 titlePanel("Silicon valley"),
 sidebarLayout(
 sidebarPanel(
 sliderInput("countInput", "count", 0, 100, c(25, 40)),
 radioButtons("jobInput", "Job category",
              choices = c("Technicians", "Professionals", "Sales workers", "Administrative support"),
              selected = "Technicians"),
 selectInput("companyInput", "company",
              choices = c("Twitter", "Uber", "View"))
),
mainPanel(
  plotOutput("coolplot"),
  br(), br(),
  tableOutput("results")
)
)
)

server <- function(input, output) {
output$coolplot <- renderPlot({
filtered <-
  bcl %>%
  filter(count == input$countInput,
         job_category == input$jobInput,
         company == input$companyInput
  )
ggplot(filtered, aes(race)) +
  geom_histogram()
})
}

shinyApp(ui = ui, server = server)

And here is the result:

enter image description here


Solution

  • Try this:

    library(shiny)
    library(ggplot2)
    library(dplyr)
    
    bcl <- read.csv(file = "reveal.csv", colClasses = c("character", "integer", "factor", "factor", "factor", "integer"), na.strings = c("na", "NA")) %>% na.omit()
    
    ui <- fluidPage(titlePanel("Silicon valley"),
                    sidebarLayout(
                      sidebarPanel(
                        sliderInput("countInput", "count", 0, 100, c(0, 100)),
                        radioButtons(
                          "jobInput",
                          "Job category",
                          choices = c(
                            "Technicians",
                            "Professionals",
                            "Sales workers",
                            "Administrative support"
                          ),
                          selected = "Technicians"
                        ),
                        selectInput("companyInput", "company",
                                    choices = c("Twitter", "Uber", "View"))
                      ),
                      mainPanel(plotOutput("coolplot"),
                                br(), br(),
                                tableOutput("results"))
                    ))
    
    server <- function(input, output) {
      output$coolplot <- renderPlot({
        filtered <-
          bcl %>%
          filter(
            count == input$countInput,
            job_category == input$jobInput,
            company == input$companyInput
          )
        ggplot(filtered, aes(race)) +
          geom_bar()
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    I have changed geom_histogram to geom_bar since it is a better option for your data. Let me know what you think.