【问题标题】:Multiple Input selection in RshinyR Shiny中的多输入选择
【发布时间】:2019-08-21 23:02:23
【问题描述】:

我正在尝试为我的项目构建一个 Rshiny。我正在尝试包含 2 个输入, First Input 是一个下拉列表,其中包含从 1 月到 12 月的月份。 第二个输入可以是下拉列表或任何允许我选择多个项目的内容。第二个输入项是-“Assault”,“Theft”,“Burgary”等。 用户只能从第一个输入中选择一个项目,从第二个输入中选择多个项目。 所以基本上,如果我从第一个输入中选择 Jan 并从第二个输入中选择“Assault”,“Theft”,则应该显示一个条形图,描述 Assault 和 Theft In jan 的数量。

输入数据框如下所示 PrimaryType Month count Assault Jan 25 Burglary Jan 30 Tresspass Feb 23 Assault Feb 12 Burglary Feb 34

ui <- fluidPage(


 titlePanel(div(HTML("<em> Crime Rate in Chichago </em>"))),

  tabsetPanel(



  tabPanel(" Frequency of Crime ",
         sidebarLayout(

           sidebarPanel(

             #First Input#

             selectInput(inputId = "Month",
                         label = " Choose a Month",
                         choices = unique(crime$Month))),

           #Second input#
           selectInput(inputId = "Crime",
                       label = " Choose among Crime",
                       choices = unique(crime$`Primary Type`),multiple = 
TRUE)),

         #Output#

           mainPanel = (plotOutput("view")
           )
         )
  )
  )


  #Shiny app -Server#
server <- function(input, output) {
  output$view <- renderPlot({
  Monthfilter <- a[a$Month==input$Month,]

crimefilter <- Monthfilter[Monthfilter$`Primary Type` %in% input$Crime,]

ggplot(crimefilter, aes(`Primary Type`,c))+geom_bar(stat="identity")
  })
}

#Shinyapp#
shinyApp(ui = ui, server = server)

我想要一个包含多种主要犯罪类型的特定月份的条形图

【问题讨论】:

    标签: r shiny shinydashboard shiny-reactivity


    【解决方案1】:

    以后,请尝试在您的代码中提供一些示例数据,这样就不必再复制了。

    以下是您的代码中的一些错误:

    • mainPanel 应该属于 sidebarLayout
    • 您的数据集是“犯罪”而不是“a”,正如您在绘图输出中所输入的 (a[a$Month==input$Month,])
    • 您可能在数据集中将列标题设置为“PrimaryType”,但在代码中将其引用为“Primary Type”(带空格)
    crime = data.table('Primary Type' = c('Assault','Burglary', 'Tresspass', 'Assault','Burglary'),
                       Month = c('Jan', 'Jan', 'Feb','Feb','Feb'),
                       Count = c(12,13,43,54,11))
    
    ui <- fluidPage(
    
    
      titlePanel(div(HTML("<em> Crime Rate in Chichago </em>"))),
    
      tabsetPanel(
    
    
    
        tabPanel(" Frequency of Crime ",
                 sidebarLayout(
    
                   sidebarPanel(
                     #First Input#
    
                     selectInput(inputId = "Month",
                                 label = " Choose a Month",
                                 choices = unique(crime$Month)),
    
                     #Second input#
                     selectInput(inputId = "Crime",
                                 label = " Choose among Crime",
                                 choices = unique(crime$`Primary Type`),
                                 multiple = TRUE)
                     ),
    
                   #Output#
    
                   mainPanel(
                     plotOutput("bar_plot")
                   )
    
                  )
    
    
        )
      )
    )
    
    
    #Shiny app -Server#
    server <- function(input, output) {
    
      output$bar_plot <- renderPlot({
        Monthfilter <- crime[crime$Month == input$Month,]
        print('hello')
        crimefilter <- Monthfilter[Monthfilter$`Primary Type` %in% input$Crime,]
    
        ggplot(data = crimefilter, aes(x = `Primary Type`, y = Count)) + geom_bar(stat="identity")
      })
    }
    
    #Shinyapp#
    shinyApp(ui = ui, server = server)r)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 2017-04-21
      • 1970-01-01
      • 2020-06-27
      • 2016-12-30
      • 2019-06-29
      • 1970-01-01
      相关资源
      最近更新 更多