【问题标题】:reactive tabPanel in a navbarMenu with DT package带有 DT 包的 navbarMenu 中的反应式 tabPanel
【发布时间】:2016-03-12 09:33:38
【问题描述】:

我想创建一个带有响应式 tabPanel/tabPanels 的简单应用程序,这将取决于 selectInput 中的值(我已经找到了解决方案 here)。此外,在此小部件中选择一个值后,我将看到不同数量的 tabPanel,它们也应该用作过滤器。 例如。在我的应用程序中,我使用 diamonds 数据集。如果我选择“非常好”这个词,我将看到一个数据集,其中所有行都具有该值。在它的顶部,我还将看到过滤数据集中所有唯一的 color 值。我想要实现的是有可能使用上面的 tabPanels 再次过滤。

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)
    })
  }
))

【问题讨论】:

    标签: r shiny navbar dt


    【解决方案1】:

    经过几个小时的搜索和尝试不同的配置,我创建了我想要实现的目标。

    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))
            })
          }
        })
      }
    ))
    

    【讨论】:

      猜你喜欢
      • 2014-06-25
      • 2018-10-30
      • 1970-01-01
      • 2021-05-26
      • 2012-04-13
      • 1970-01-01
      • 2018-12-18
      • 2020-11-20
      • 1970-01-01
      相关资源
      最近更新 更多