2017-12-27 1016 views
1

我有一個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

+0

不要發佈數據圖片,請使用R.中的'dput'函數發佈數據到您的問題中。數據的一個子集通常很好。 – jsb

+0

在R控制檯中寫入'?dput'以打開幫助頁面。 – jsb

+0

在你的問題中粘貼'dput'的輸出。切勿將數據發佈爲圖片,因爲它無法複製。請刪除它們並使用'dput'。 – jsb

回答

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_histogramgeom_bar,因爲它是爲您的數據提供更好的選擇。讓我知道你的想法。

+0

謝謝,它的工作原理,但似乎我應該修改一些代碼,因爲如果我更改公司或更改工作類別,它不能正常工作 – Viola

+0

確切地說,一些事情要繼續努力:-)保持編碼! – jsb