我正在尝试为我的项目构建Rshiny。我试图包括2个输入, First Input是一个下拉列表,其中包含从1月到12月的几个月。 第二个输入可以是下拉菜单,也可以是允许我选择多个项目的任何输入。第二个输入项是-“突击”,“盗窃”,“盗窃”等。 用户只能从“第一输入”中选择一项,而从“第二输入”中选择多项。 因此,基本上,如果我从第一个输入中选择Jan并在第二个输入中选择“ Assault”,“ Theft”,则应显示描述jan的“ Assault and Theft In”编号的条形图。
输入数据框如下所示
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)
我想要一个特定月份有多种主要犯罪类型的条形图
答案 0 :(得分:0)
将来,请尝试在您的代码中提供一些示例数据,这样就不必复制它们。
以下是您的代码中的一些错误:
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)