Diagram doesn't show in Shiny

r shiny diagram

61 观看

1回复

227 作者的声誉

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

作者: Viola 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

1627 作者的声誉

决定

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.

作者: jsb 发布者: 2017 年 12 月 27 日
32x32