如何选择任何年份,只显示包含所选年份的行

时间:2019-02-28 17:20:49

标签: r shiny

我正在构建一个能够查看我们媒体销售情况的闪亮应用程序。 我的数据在一个csv文件中

我希望能够选择任何年份,并且只显示具有所选年份的行。

如图所示。

Type Erasure

Desired output

有人可以帮助解决服务器停顿声明

media <- read.csv("media.csv",stringsAsFactors=FALSE)

State,Year,DVD,BluRay,Download
CT,2013,265,95,141
CT,2014,201,54,65
CT,2015,154,62,28
CT,2016,96,23,72
CT,2017,49,84,36
MA,2013,116,321,108
MA,2014,66,119,145
MA,2015,69,64,121
MA,2016,84,81,210
MA,2017,79,35,96
MD,2013,161,36,26
MD,2014,24,97,84
MD,2015,201,74,24
MD,2016,254,74,154
MD,2017,95,63,247
NJ,2013,78,60,168
NJ,2014,201,85,321
NJ,2015,209,75,245
NJ,2016,217,55,88
NJ,2017,65,46,71
PA,2013,94,95,68
PA,2014,232,91,94
PA,2015,154,73,203
PA,2016,87,101,119
PA,2017,200,98,149

代码:

library(shiny)
ui <- fluidPage(

  titlePanel('DVD/BluRay/Download:'),
  sidebarLayout(
    sidebarPanel(
      selectInput("State", label = h4("Which State are you in:"),choices =media$State),
      checkboxGroupInput("Category", label = h4("Category"), 
                         choices = list("DVD" , "BluRay" , "Download" ),
                         selected = list("DVD" , "BluRay" , "Download" )),
      checkboxGroupInput("Year", label = h4("Which Year(s)"),choices = unique(media$Year))
      ),
    mainPanel(
      tableOutput("mediadata")
    )
  )
)

server <- function(input, output) {
   output$mediadata <- renderTable({
     statefilter <- subset(media, media$State == input$State)
     statefilter[c('State', 'Year', input$Category)]
  })
}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

现在可以使用:

  output$mediadata <- renderTable({
    statefilter <- subset(media[media$State == input$State & media$Year %in% input$Year,])
    statefilter[c('State', 'Year', input$Category)]
})