【问题标题】:How do I select any year and only have the row with the selected year be displayed如何选择任何年份并且只显示所选年份的行
【发布时间】:2019-02-28 17:20:49
【问题描述】:

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

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

如图所示。

有人可以帮助解决服务器输出声明

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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    现在可以了:

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

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 2018-03-02
      • 2018-03-31
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多