Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
shahr43 opened this issue Sep 25, 2024 · 1 comment
Open
3 tasks done
Labels
enhancement New feature or request

Comments

@shahr43
Copy link

shahr43 commented Sep 25, 2024

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

  • I agree to follow this project's Code of Conduct.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines.

Security Policy

  • I agree to follow this project's Security Policy.
@shahr43 shahr43 added the enhancement New feature or request label Sep 25, 2024
@gogonzo
Copy link
Contributor

gogonzo commented Sep 25, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants