R Shiny 应用程序中的外部过滤器

罗斯

我有下表(为简洁起见,省略了许多组合):

Name      r2     pvalue    t-statistic
a1
b1
c1
a1 & b1
a1 & c1
b1& c1
a1 & b1 & c1
....

其中 a1、b1 和 c1 是从向量创建的

a = c("a1", "a2")
b = c("b1","b2","b3")
c = c("c1")

我想创建一个与链接https://shiny.rstudio.com/gallery/basic-datatable.html中的那个完全一样的闪亮表,除了一个问题。在该示例中,过滤器实际上是表中的列,而我希望能够选择“a1”和“c1”并仅获取带有 a1 和 c1 的列。我基本上希望能够使用显示的向量在名称列中查找包含我选择的值的字符串。有谁知道如何做到这一点?我发现的所有示例都使用已经是表中列的过滤器。

维舍什·施里瓦斯塔夫

这是基于您的示例数据的解决方案。您可以通过更改 filter() 函数内部的逻辑来更改过滤条件。

library(shiny)
library(dplyr)
library(data.table)

a = c("a1", "a2")
b = c("b1","b2","b3")
c = c("c1")

# Create dummy data
Name <- c("a1", "b1", "c1", "a1 & b1", "a1 & c1", "b1& c1", "a1 & b1 & c1")
# Random numbers
r2 <- runif(length(Name))
p.value <- runif(length(Name))
t.statistic <- runif(length(Name))

dummy.df <- cbind.data.frame(Name, r2, pvalue, t.statistic)

# Define UI
#ui <- fluidPage(
 # sidebarPanel(
  #  selectInput("a.list", "Select As", a),
   # selectInput("b.list", "Select Bs", b),
   # selectInput("c.list", "Select Cs", c)
  #),
  #mainPanel(
   # tableOutput("tab1"),
    #tableOutput("tab2")
  #)

    # Create a new Row in the UI for selectInputs
  fluidRow(
    column(4, selectInput("a.list", "Select As", a)
    ),
    column(4, selectInput("b.list", "Select Bs", b)
    ),
    column(4, selectInput("c.list", "Select Cs", c)
    )
  ),
  # Create a new row for the table.
  fluidRow(
    column(8, tableOutput("tab2"))
  )
)

# Define server logic
server <- function(input, output){
  # Table with all the data
  output$tab1 <- renderTable(dummy.df)

  # Apply filter to data
  foo <- reactive({
    dummy.df %>%
    filter(Name %like% input$a.list & Name %like% input$c.list)
  })

  # Table with filtered data - returns rows 5 and 7
  output$tab2 <- renderTable(foo())
}

# Create shiny app
shinyApp(ui = ui, server = server)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章