【问题标题】:How to make kable table reactive() in shiny app? Shiny + kable如何在闪亮的应用程序中制作 kable 表反应()?闪亮 + kable
【发布时间】:2018-12-29 20:27:58
【问题描述】:

我正在尝试使 kable 表具有响应性并将其导出到闪亮的应用程序中。已经在服务器内部尝试了renderDataTable/renderTable,输出函数为datatableOutput/tableOutput,但运气不好,下面是代码行。

  output$tableset <- renderDataTable({
kable(spread_bole) %>%
  kable_styling(font_size = 15 ,bootstrap_options = c("striped","hover", "condensed")) })

tableOutput("tableset")      

【问题讨论】:

    标签: r shiny kable kableextra


    【解决方案1】:

    你好一直在寻找相同的东西,我发现 this 可以进行一些更改

    library(shiny)
    library(tibble)
    library(dplyr)
    library(kableExtra)
    
    data("mtcars"); head(mtcars,2)
    mtcars <- rownames_to_column(mtcars, var="car") %>% head
    
    
    ui <- fluidPage(
      
      # Application title
      titlePanel("mtcars"),
      
      sidebarLayout(
        sidebarPanel(
          sliderInput("mpg", "mpg Limit",
                      min = 11, max = 33, value = 20)
        ),
        
        mainPanel(
          tableOutput("mtcars_kable")
        )
      )
    )
    
    server <- function(input, output) {
    
      output$mtcars_kable <- function() {
        req(input$mpg)
          mtcars %>%
          #dplyr::mutate(car = rownames(.)) %>% 
          dplyr::select(car, everything()) %>%
          dplyr::filter(mpg <= input$mpg) %>%
          knitr::kable("html") %>%
          kable_styling("striped", full_width = F) %>%
          add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6))
      }
    }
    
    # Run the application
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      【解决方案2】:

      由于kable 返回 HTML,您可以使用ui 中的htmlOutputserver 中的renderText 呈现您的表格:

      # UI component
      htmlOutput("tableset") 
      
      # server component
      output$tableset <- renderText({
        kable(spread_bole) %>%
          kable_styling(
            font_size = 15,
            bootstrap_options = c("striped", "hover", "condensed")
          ) 
      })
      

      此外,如果您想让它响应用户输入,您可以将其包装在反应式表达式中:

      my_table <- reactive({
        kable(spread_bole) %>%
          kable_styling(
            font_size = 15,
            bootstrap_options = c("striped", "hover", "condensed")
          )
      })
      
      # my_table() will call the cached table 
      

      如果您想多次使用同一个表,这将特别有用。您也可以查看eventReactive 以使用特定输入触发它。有关 Shiny 反应性的更多信息,请参阅此处:https://shiny.rstudio.com/articles/reactivity-overview.html

      【讨论】:

        猜你喜欢
        • 2017-07-27
        • 1970-01-01
        • 2013-07-16
        • 2013-07-08
        • 1970-01-01
        • 1970-01-01
        • 2015-09-11
        • 2016-05-23
        • 1970-01-01
        相关资源
        最近更新 更多