Ross
Ross

Reputation: 73

External Filters in R Shiny Application

I have the following table (left many of the combinations out for brevity):

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

Where a1, b1, and c1, were created from the vectors

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

I want to create a Shiny table exactly like the one in the link https://shiny.rstudio.com/gallery/basic-datatable.html except for one issue. In that example the filters are actually columns in the table whereas I want to be able to choose "a1" and "c1" and get only the ones with a1 and c1. I essentially want to be able to use the vectors shown to find strings in the name column that contain the values I select. Does anyone know how to do this? All the examples I have found use filters that are already columns in the table.

Upvotes: 0

Views: 68

Answers (1)

Vishesh Shrivastav
Vishesh Shrivastav

Reputation: 2129

Here's a solution based on your example data. You can change the filtering conditions by changing the logic inside filter() function.

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)

Upvotes: 1

Related Questions