【问题标题】:Diagram doesn't show in Shiny图表未在 Shiny 中显示
【发布时间】:2017-12-27 17:45:59
【问题描述】:

我有一个 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)

结果如下:

【问题讨论】:

  • 请使用 R 中的 dput 函数,而不是发布数据的图片。在您的问题中发布数据。数据的一个子集通常很好。
  • 在 R 控制台中写 ?dput 以打开帮助页面。
  • dput 的输出粘贴到您的问题中。切勿将数据作为图片发布,因为它无法复制。请删除它们并使用dput
  • 好的,但是我的数据有 3960 条记录
  • 然后使用子集或使其在 Dropbox 上可用。

标签: r shiny diagram


【解决方案1】:

试试这个:

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,因为它是您数据的更好选择。让我知道你的想法。

【讨论】:

  • 谢谢,可以了,不过好像要稍微修改一下代码,因为换公司或者换工作类别就不行了
  • 没错,有几件事需要继续努力 :-) 继续编码!
猜你喜欢
  • 2020-10-28
  • 1970-01-01
  • 2016-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 2017-03-07
  • 1970-01-01
相关资源
最近更新 更多