取消选中checkboxGroupInput时如何隐藏/取消隐藏列

时间:2019-02-25 22:32:27

标签: r shiny

我刚刚学习Shiny,并且有数据发送到tableOutput。

我有3个复选框选择:“ CarA”,“ CarB”,“ CarC”。我的表格显示了按县列出的当年汽车销量。

我期望的动作和结果是:

  • 操作:取消选中CarA。 结果:该表仅呈现“县”,“年”,“ CarB”,“ CarC”列。
  • 操作:取消选中CarB。 结果:该表仅呈现“县”,“年”,“ CarA”,“ CarC”列。
  • 操作:取消选中CarC。 结果:该表仅呈现“县”,“年”,“ CarA”和“ CarB”列。
  • 操作:取消选中CarA,CarB,CarC。结果:表仅显示“县”,“年”列。

我的数据在一个名为data.csv的.csv文件中。

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

County,Year,CarA,CarB,CarC
Allen,2014,8,36,49
Allen,2015,15,45,45
Allen,2016,22,54,24
Allen,2017,9,32,61
Allen,2018,16,88,98
Alon,2014,12,66,11
Alon,2015,45,84,19
Alon,2016,35,23,65
Alon,2017,32,124,35
Alon,2018,65,25,95
Brome,2014,51,36,84
Brome,2015,12,48,12
Brome,2016,48,95,36
Brome,2017,65,64,18
Brome,2018,65,21,65
Catgus,2014,34,95,17
Catgus,2015,62,36,36
Catgus,2016,86,59,26
Catgus,2017,88,85,35
Catgus,2018,15,32,32
Cay,2014,26,84,64
Cay,2015,45,12,85
Cay,2016,84,36,38
Cay,2017,69,18,9
Cay,2018,24,35,168

代码:

#carsales
library(shiny)

ui <- fluidPage(

  titlePanel('Car Sales:'),
  sidebarLayout(
    sidebarPanel(
      selectInput("county", label = h4("Select a County:"),
                  choices = county$County),
      checkboxGroupInput("Category", label = h4("Category"), 
                         choices = list("CarA", "CarB", "CarC"),
                         selected = list('CarA', 'CarB', 'CarC'))
    ),
    mainPanel(
      tableOutput("cardate")
    )
  )
)

server <- function(input, output) {

  output$cardate <- renderTable({
    countyfilter <- subset(county, county$County == input$county)
  })
}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

您快到了。不知道是什么使您无法完成它。询问您是否需要澄清

output$cardate <- renderTable({
countyfilter <- subset(county, county$County == input$county)
countyfilter[,c('County','Year', input$Category)]
 })
相关问题