How to filter and retrieve rows using numericInput in R Shiny App

Hi,

I am working with R Shiny App and interested in extracting features with values passing a certain threshold. For instance; extracting only features passing the threshold that is value >= 5000 or >= 10000, etc., across all columns. I have shown an example app.R shiny script, and adding the filter in the server doesn't help. Is there a way to revise the below script and make it interactive? This would be helpful

library(shiny)
library(DT)

Data <- structure(list(Col1_Counts = c(100L, 10L, 2000L, 0L, 2000L, 0L, 
                               11L, 15L, 19L, 0L, 100L, 50L, 10L, 100L, 50L), CSC1_Counts = c(150L, 
                                                                                              50L, 150L, 3L, 50L, 0L, 12L, 16L, 20L, 23L, 1000L, 50L, 10L, 
                                                                                              50L, 50L), BC_Counts = c(50L, 75L, 100L, 10L, 75L, 0L, 13L, 17L, 
                                                                                                                       21L, 0L, 100000L, 40L, 10L, 100000L, 50L)), class = "data.frame", row.names = c("Feature_1", 
                                                                                                                                                                                                       "Feature_2", "Feature_3", "Feature_4", "Feature_5", "Feature_6", 
                                                                                                                                                                                                       "Feature_7", "Feature_8", "Feature_9", "Feature_10", "Feature_11", 
                                                                                                                                                                                                       "Feature_12", "Feature_13", "Feature_14", "Feature_15"))


ui <- fluidPage(
      actionButton("go", "Filter Read Counts"),
      numericInput("n", "Counts", 5000),
      conditionalPanel(
        'input.dataset === "Data"',
        helpText("Data - test")
      ),
    mainPanel(
      tabsetPanel(
        id = 'dataset',
        tabPanel("Data", DT::dataTableOutput("mytable1"))
      )
    )
)

server <- function(input, output) {
  
  randomVals <- eventReactive(input$go, {
    runif(input$n)
  })
  output$mytable1 <- DT::renderDataTable({
    DT::datatable(Data, options = list(orderClasses = TRUE)) 
    
  })
  
}

shinyApp(ui, server)

To filter data:

Data %>%
      filter(if_all(everything(), ~ . >= 5000))

Thank you,
Toufiq

library(shiny)
library(DT)
library(tidyverse)

Data <- structure(list(Col1_Counts = c(
  100L, 10L, 2000L, 0L, 2000L, 0L,
  11L, 15L, 19L, 0L, 100L, 50L, 10L, 100L, 50L
), CSC1_Counts = c(
  150L,
  50L, 150L, 3L, 50L, 0L, 12L, 16L, 20L, 23L, 1000L, 50L, 10L,
  50L, 50L
), BC_Counts = c(
  50L, 75L, 100L, 10L, 75L, 0L, 13L, 17L,
  21L, 0L, 100000L, 40L, 10L, 100000L, 50L
)), class = "data.frame", row.names = c(
  "Feature_1",
  "Feature_2", "Feature_3", "Feature_4", "Feature_5", "Feature_6",
  "Feature_7", "Feature_8", "Feature_9", "Feature_10", "Feature_11",
  "Feature_12", "Feature_13", "Feature_14", "Feature_15"
))


ui <- fluidPage(
  actionButton("go", "Filter Read Counts"),
  numericInput("n", "Counts", 0),
  conditionalPanel(
    'input.dataset === "Data"',
    helpText("Data - test")
  ),
  mainPanel(
    tabsetPanel(
      id = "dataset",
      tabPanel("Data", DT::dataTableOutput("mytable1"))
    )
  )
)

server <- function(input, output) {
  cut_off_val <- eventReactive(input$go, {
    req(input$n)
  },ignoreNULL = FALSE)

  filtered_df <- reactive({
    Data %>%
      filter(if_all(everything(), ~ . >= req(cut_off_val())))
  })

  output$mytable1 <- DT::renderDataTable({
    DT::datatable(req(filtered_df()), options = list(orderClasses = TRUE))
  })
}

shinyApp(ui, server)
1 Like

@nirgrahamuk Perfect, thank you very much. This was the intended output I was interested and this helped me to add more tables using tabsetPanel. Btw, just to complement table that we discussed, in tabsetPanel of the same page I wanted to add another table to visualize the statistical analysis table with 2 filters one for the FC column and another for p.value column. Is this straightforward to add? Here is the example;

FC column filter = probably, numericInput boxes to consider the range or a slider (the rows passing >+2 and >-2)
p.value = here I want to consider numericInput starting cut-off would be <=0.05 which are significant) (the rows passing <=0.05)

dput(Stats_Output)
structure(list(FC = c(1, 2, 3, -1, -2.06, -3.09, -10, -20, -1, 
                      -2, -3, 0.75, -0.89, -0.1, 10), p.value = c(0.005, 0.001, 0.05, 
                                                                  0.06, 0.059, 0.8, 0.9, 1, 0.01, 0.69, 0.023, 0.1, 1e-04, 1e-04, 
                                                                  0.03)), class = "data.frame", row.names = c("Feature_1", "Feature_2", 
                                                                                                              "Feature_3", "Feature_4", "Feature_5", "Feature_6", "Feature_7", 
                                                                                                              "Feature_8", "Feature_9", "Feature_10", "Feature_11", "Feature_12", 
                                                                                                              "Feature_13", "Feature_14", "Feature_15"))
#>                FC p.value
#> Feature_1    1.00  0.0050
#> Feature_2    2.00  0.0010
#> Feature_3    3.00  0.0500
#> Feature_4   -1.00  0.0600
#> Feature_5   -2.06  0.0590
#> Feature_6   -3.09  0.8000
#> Feature_7  -10.00  0.9000
#> Feature_8  -20.00  1.0000
#> Feature_9   -1.00  0.0100
#> Feature_10  -2.00  0.6900
#> Feature_11  -3.00  0.0230
#> Feature_12   0.75  0.1000
#> Feature_13  -0.89  0.0001
#> Feature_14  -0.10  0.0001
#> Feature_15  10.00  0.0300

Created on 2023-03-02 with reprex v2.0.2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.