在Shiny中过滤表格

时间:2015-08-25 11:36:33

标签: r shiny

给出了以下数据集(实际上更多情况):

data_test = data.frame(ID = c ("1","2","3","4","5"),
              product = c("A","B","C","A","C"),
              milieu = c("good","medium","bad","medium","bad"),
              online = c(1,0,1,1,0),
              ooh = c(0,1,0,1,1),
              event = c(1,1,0,0,0))

现在我想建立一个闪亮的应用程序,有人可以选择一个环境,让我们说“好”和产品“A”,所有在线的“1”和具有这些设置的数据表都会被给出。在示例ID 1中。

我尝试了以下

UI:

shinyUI(fluidPage(
  titlePanel("product milieu"),

  sidebarLayout(
    sidebarPanel("select",
                 selectInput("select_milieu",
                             label = "Milieu",
                 choices = list("good",
                                "medium",
                                "bad")
                 ),
                 selectInput("select_product", 
                             label = "Product",
                             choices = list("A", 
                                            "B", 
                                            "C")
                 ),
                 selectInput("select_online", 
                             label = "Online",
                             choices = list(1, 
                                            0)
                 ),
                 selectInput("select_ooh", 
                             label = "ooh",
                             choices = list(1, 
                                            0)
                 ),
                 selectInput("select_Event", 
                             label = "Event",
                             choices = list(1, 
                                            0)

                 )
    ),
    mainPanel("My table",
              textOutput("output_milieu"),
              textOutput("output_product"),
              textOutput("output_event"),
              textOutput("output_online"),
              textOutput("output_ooh"),
              tableOutput("gapminder_table")
              )
  )
))

服务器:

shinyServer(function(input, output) {

  output$gapminder_table <- renderTable({ 
    subset(data_test,
           milieu == input$select_milieu & product == input$select_product &
             online == input$select_online)

  })
  output$output_milieu <- renderText({
    paste("milieu", input$select_milieu)
  })
  output$output_product <- renderText({
    paste("product", input$select_product)
  })
  output$output_event <- renderText({
    paste("Event", input$select_Event)
  })
  output$output_online <- renderText({
    paste("Online", input$select_Online)
  })
  output$output_ooh <- renderText({
    paste("out of Home", input$select_ooh)
  })

})

我现在的问题是如何过滤“event”和“ooh”。有人有建议吗?

提前致谢!

1 个答案:

答案 0 :(得分:7)

如果您开始探索带有光泽的数据表的DT包,则可以使这更加简单。有了这个,您只需在相应的列上方输入您喜欢的任何过滤条件。

server.R

library(shiny)
library(DT)

data_test = data.frame(ID = c ("1","2","3","4","5"),
                       product = c("A","B","C","A","C"),
                       milieu = c("good","medium","bad","medium","bad"),
                       online = c(1,0,1,1,0),
                       ooh = c(0,1,0,1,1),
                       event = c(1,1,0,0,0))

shinyServer(function(input, output) {

    output$gapminder_table <- renderDataTable({ 
        data_test        
    },
    filter = 'top',
    rownames = FALSE)    
})

ui.R

library(shiny)
library(DT)

shinyUI(fluidPage(
    titlePanel("product milieu"),

    sidebarLayout(
        sidebarPanel("Place for other criteria"
        ),
        mainPanel("My table",
                  dataTableOutput("gapminder_table")
        )
    )
))