图表并没有在Shiny中显示

时间:2017-12-27 17:45:59

标签: r shiny diagram

我有一个Shiny应用程序,显示了硅谷公司的种族和员工数量之间的关系。左侧的工具栏可见,但图表未显示。我该如何更改代码?

以下是代码:

 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)

结果如下:

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个:

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)

我已将geom_histogram更改为geom_bar,因为它是您数据的更好选择。让我知道你的想法。