【问题标题】:Filter a table in Shiny在 Shiny 中过滤表格
【发布时间】:2015-08-25 20:15:34
【问题描述】:

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

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 中。

我尝试了以下

用户界面:

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)
  })

})

我现在的问题是如何过滤“事件”和“哦”。有人给点建议吗?

提前致谢!

【问题讨论】:

  • 为什么不在同一个renderTable() 步骤中进行所有过滤?
  • 另外,您没有显示您使用renderTable() 步骤制作的表格是否有原因?你的意思是在你的 ui 中有一个 tableOutput("gapminder_table") 行吗?
  • 对不起,我在复制/粘贴过程中忘记了这一点...您将如何在 renderTable() 中进行过滤??
  • 和你现在做的一样。我会发布一个答案,以便您了解我的意思。
  • 附带说明:?subset 旨在用于交互使用。请参阅帮助文件中的警告部分。使用[ 或例如dplyr 的filter_ 函数可能会更好。

标签: r shiny


【解决方案1】:

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

服务器.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")
        )
    )
))

【讨论】:

  • 这很简单!
猜你喜欢
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 2021-11-25
  • 1970-01-01
  • 2020-08-15
  • 1970-01-01
  • 2017-04-30
相关资源
最近更新 更多