簡體   English   中英

R Shiny應用程序中的外部濾波器

[英]External Filters in R Shiny Application

我有下表(為簡便起見,省略了許多組合):

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中的表完全相同的Shiny表。 在該示例中,過濾器實際上是表中的列,而我希望能夠選擇“ 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM