0

I would like to create an simple app with reactive tabPanel/tabPanels which will depends on value in selectInput (I already found a solution here). Furthermore, after I choose one value in this widget I will see different number of tabPanels which also should work as a filter. E.g. in my app I use diamonds dataset. If I choose a word 'Very Good' I will see a dataset with all rows with this value. In the top of it I will also see all unique color values in filtered dataset. What I want to achieve is to get a possibility to filter once again using tabPanels above.

library(shiny)
library(shinyTree)
library(dplyr)
library(DT)
library(ggplot2)

diamonds_test <- sample_n(diamonds, 100)
diam_cut <- 
  list(
    `Very Good` = "Very Good",
    Ideal = "Ideal",
    Fair = "Fair",
    Premium = "Premium",
    Good = "Good"
  )

runApp(list(
  ui = pageWithSidebar(
    headerPanel('Dynamic Tabs'),
    sidebarPanel(
      selectInput('name','',choices = diam_cut)
    ),
    mainPanel(
      uiOutput('mytabs'),
      dataTableOutput('table')
    )
  ),
  server = function(input, output, session){

    output$mytabs = renderUI({
      colorVector <- diamonds_test %>%
        filter(cut == input$name) %>% 
        distinct(color) %>% 
        .[['color']] %>% 
        as.character()

      myTabs = lapply(colorVector, tabPanel)
      do.call(tabsetPanel, c(myTabs, type = 'pills'))
    })

    output$table <- renderDataTable({
      data <- diamonds_test %>%
        filter(cut == input$name)
      datatable(data)
    })
  }
))
Community
  • 1
  • 1
Nicolabo
  • 1,337
  • 12
  • 30

1 Answers1

0

After a few hours of searching and trying different configuration I created what I want to achieve.

library(shiny)
library(shinyTree)
library(dplyr)
library(DT)

diamonds_test <- sample_n(diamonds, 100)
diam_cut <- 
  list(
    `Very Good` = "Very Good",
    Ideal = "Ideal",
    Fair = "Fair",
    Premium = "Premium",
    Good = "Good"
  )

runApp(list(
  ui = pageWithSidebar(
    headerPanel('Dynamic Tabs'),
    sidebarPanel(
      selectInput('name','',choices = diam_cut)
    ),
    mainPanel(
      uiOutput('mytabs')
    )
  ),
  server = function(input, output, session){

    colorVector <- reactive({
      colorVector <- diamonds_test %>%
        filter(cut == input$name) %>% 
        distinct(color) %>% 
        .[['color']] %>% 
        as.character()
    })

    output$mytabs <- renderUI({
      colorVector_use <- colorVector()
      myTabs = lapply(colorVector_use, tabPanel)

      do.call(tabsetPanel,
              c(type = 'pills',
                lapply(colorVector_use, function(x) {
                  call("tabPanel",x ,call('dataTableOutput',paste0("table_",x)))
                })
              ))
    })

    data <- reactive({
      df <- diamonds_test %>% 
        filter(cut == input$name)
    })

    observe({
      if (!is.null(colorVector())){
        lapply(colorVector(), function(color_value){
          output[[paste0('table_',color_value)]] <- renderDataTable(
            data() %>% filter(color == color_value))
        })
      }
    })
  }
))
Nicolabo
  • 1,337
  • 12
  • 30