insightsengineering / teal.slice

Reproducible slice module for teal applications
https://insightsengineering.github.io/teal.slice/
Other
11 stars 5 forks source link

[Feature Request]: Master Filter for common columns across datasets #620

Open shahr43 opened 1 month ago

shahr43 commented 1 month ago

Feature description

Hello, we have a use case where we need to filter across datasets. I have modified the example from the vignette to explain what I mean.

library(shiny)
library(teal.slice)

df1 <- data.frame(col1 = c(2, 3, 5, 7, 9), col2 = rnorm(5))
df2 <- data.frame(col1 = 1:5, col3 = rnorm(5))
# create a FilteredData object
datasets <- init_filtered_data(list(df1 = df1, df2 = df2))

# setting initial state
set_filter_state(
  datasets = datasets,
  filter = teal_slices(
    teal_slice(dataname = "df1", varname = "col1"),
    teal_slice(dataname = "df2", varname = "col2"),
    count_type = "all",
    allow_add = TRUE
  )
)

ui <- fluidPage(
  fluidRow(
    column(
      width = 9,
      tabsetPanel(
        tabPanel(title = "df1", dataTableOutput("df1_table")),
        tabPanel(title = "df2", dataTableOutput("df2_table"))
      )
    ),
    # ui for the filter panel
    column(width = 3, datasets$ui_filter_panel("filter_panel"))
  )
)

server <- function(input, output, session) {
  # this is the shiny server function for the filter panel and the datasets
  # object can now be used inside the application
  datasets$srv_filter_panel("filter_panel")

  # get the filtered datasets and put them inside reactives for analysis
  df1_filtered_data <- reactive(datasets$get_data(dataname = "df1", filtered = TRUE))
  df2_filtered_data <- reactive(datasets$get_data(dataname = "df2", filtered = TRUE))

  output$df1_table <- renderDataTable(df1_filtered_data())
  output$df2_table <- renderDataTable(df2_filtered_data())
}

shinyApp(ui, server)

Here as you can see there is a common column indf1 and df2 i.e col1. We need to have a "master filter" in place which shows this common column once for both the datasets. It will have range from 1 to 9 since that is the min and max range combined in both the datasets and whatever the filter values are selected here it will be applied to both the datasets and they will be filtered by the value.

Let me know if you have any questions or my explanation is unclear.

Do you think this is possible to be done using teal.slice ?

Code of Conduct

Contribution Guidelines

Security Policy

gogonzo commented 1 month ago

Thank you @shahr43 for a precise example and a question.

Unfortunately, your problem can't be solved by teal.slice. Each filter is associated with particular column in a dataset, therefore it can't filter multiple dataset in the same time. This relationship with the particular column is reflected in counts. We didn't predicted scenario you are describing and I think it is a interesting feature request (to apply filter to all or specified datasets).

Fortunately, teal.slice can apply a "relational" filter. When you provide join_keys, you're able to filter child-datasets based on a filter applied in a parent-dataset. See below:

library(shiny)
library(teal.slice)
library(teal.data)

df1 <- data.frame(col1 = c(2, 3, 5, 7, 9), col2 = rnorm(5))
df2 <- data.frame(col1 = 1:5, col3 = rnorm(5))
master_df <- data.frame(col1 = union(df1$col1, df2$col1)) # create a parent object

datasets <- init_filtered_data(
  list(df1 = df1, df2 = df2, master_df = master_df),
  # link parent object with its childs by "col1"
  join_keys = join_keys(
    join_key("master_df", "df1", keys = "col1"),
    join_key("master_df", "df2", keys = "col1")
  )
)
# setting initial state
set_filter_state(
  datasets = datasets,
  filter = teal_slices(
    teal_slice(dataname = "master_df", varname = "col1"),
    teal_slice(dataname = "df2", varname = "col2"),
    count_type = "all",
    allow_add = TRUE
  )
)

ui <- fluidPage(
  fluidRow(
    column(
      width = 9,
      tabsetPanel(
        tabPanel(title = "df1", dataTableOutput("df1_table")),
        tabPanel(title = "df2", dataTableOutput("df2_table"))
      )
    ),
    # ui for the filter panel
    column(width = 3, datasets$ui_filter_panel("filter_panel"))
  )
)

server <- function(input, output, session) {
  # this is the shiny server function for the filter panel and the datasets
  # object can now be used inside the application
  datasets$srv_filter_panel("filter_panel")

  # get the filtered datasets and put them inside reactives for analysis
  df1_filtered_data <- reactive(datasets$get_data(dataname = "df1", filtered = TRUE))
  df2_filtered_data <- reactive(datasets$get_data(dataname = "df2", filtered = TRUE))

  output$df1_table <- renderDataTable(df1_filtered_data())
  output$df2_table <- renderDataTable(df2_filtered_data())
}

shinyApp(ui, server)