Changing the filter names and its values as per the values selected from another filter

I have a dataframe df. I have executed half the code like below. With this code what I get is 3 filters

Now When I select Trend in Filter1, there is a plot displayed in chart A. With this plot, when i change values in below filter(Trend), the plot is getting changed, This is fine for me.

What I want is, in Filter1 if Trend is selected, the name of the below Filter should get changed to Trend and also its values should be changed to as per whatever is given in UI.(Something like below).
image
So if I select Correlation in FIlter1 the name of the below filter should get changed to Correlation and its values as well. In other words, I do not need 3rd Filter here. Also in correlation filter, there is a combination of all possible factors. So when I select C&D the plot should show the relation between A and B and so on.

Below is the original code

   ---
  title: Current Conditions
   output: 
   flexdashboard::flex_dashboard:
   orientation: columns
   vertical_layout: scroll
   runtime: shiny
   source_code: embed
   theme: cosmo
    ---

   ```{r global}
   library(flexdashboard)
   library(readxl)
   library(tidyverse)
   library(lubridate)
   library(ggplot2)
   library(reshape)
   library(shiny)
    library(plotly)
    ```

   ```{r}
   df <- structure(list(A = c(1, 2, 3, 4, 5, 6, 7), B = c(3, 4, 5, 12, 
   23, 12, 13), C = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L), .Label = c("A", 
   "B", "C"), class = "factor"), D = structure(c(2L, 2L, 1L, 1L, 
   3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor")), class = "data.frame", row.names =             c(NA, 
   -7L))
   df <- as.data.frame(df)
   ```

   Summary
  =================

  Inputs {.sidebar}
  -----------------------------------------------------------------------

  ```{r}
    selectInput("c","Filter1",choices = c("","Trend","Correlation"))
    selectInput("b","Filter2",choices = c("ALL",levels(factor(df$C))))
    selectInput("d","Correlation",choices = c("",combn(names(df),2,paste,collapse="&")))
  ```


  Column {data-width=350}
  -----------------------------------------------------------------------

  ### Chart A

  ```{r}
  plotOutput("p1")
  output$p1 <- renderPlot({
     if (input$c == "Trend"){
    s_data <- df
      }
  if (input$c == "Trend" && input$b != "ALL") {
      s_data <- s_data %>% 
          filter(C %in% input$b)
  }
    plot(s_data$A,s_data$B)
  })
  ```


  ```

Hope my question is clear

Hi @vinayprakash808. You may use a reactive expression to assign the label of the selectInput b.

  bLabel <- reactive({
    if(isTruthy(input$c)) {
      input$c
    } else {
      "Filter2"
    }
  })
  
    selectInput("c","Filter1",choices = c("","Trend","Correlation"))
    renderUI(selectInput("b", bLabel(), choices = c("ALL",levels(factor(df$C)))))
    selectInput("d","Correlation",choices = c("",combn(names(df),2,paste,collapse="&")))

Thanks for this information. But also I need to the values to change when it turned to "Correlation". The values under correlation should be " combn(names(df),2,paste,collapse="&")"

Basically, the values should changes as per the filters.

@vinayprakash808. You can make another reactive expression to assign the choices of selectInput b according to the selection of the selectInput c.

1 Like

Perfect got it thanks.

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