【问题标题】:R Shiny renderTable - freeze columnsR Shiny renderTable - 冻结列
【发布时间】:2021-02-11 23:27:41
【问题描述】:

寻求一些帮助,在 R Shiny 中将冻结帧添加到 renderTable。我使用renderTable 而不是DTrenderDataTable,因为我有一个超过400 列的数据框。 DT 渲染时卡住了,但 renderTable 似乎工作得很快。

这是一个例子:

if (interactive()) {
  library(DT)
  
  fruit <- c("Apple", "Orange", "Pear", "Banana")
  num <- c(54, 25, 51, 32)
  Oct2020 <- c(10, 15, 20, 25)
  Nov2020 <- c(5, 7, 10, 15)
  Dec2020 <- c(7, 9, 12, 17)
  Jan2021 <- c(6, 9, 2, 0)
  Feb2021 <- c(15, 30, 12, 2)
  Mar2021 <- c(6, 7, 8, 10)
  
  data <- data.frame(fruit, num, Oct2020, Nov2020, Dec2020, Jan2021, Feb2021, Mar2021)
  
  ui <- fluidPage(
    fluidRow(
      column(width = 1, numericInput("numFruit", "Number of Fruit", value = 10)),
      column(width = 1, div(style = "margin-top: 25px", actionButton("btnUpdate", "Update")))
    ),
    
    fluidRow(
      div(style = 'height: 200px; width: 500px; overflow: scroll; font-size: 90%', align = "left", tableOutput("dt_Fruit"))  
    )
  )
  
  server <- function(input, output, session) {
    output$dt_Fruit <- renderTable(data, striped = TRUE, hover = TRUE, bordered = TRUE)
  }
  shinyApp(ui, server)
}

我正在寻找冻结前两列 fruitnum 的功能。

【问题讨论】:

    标签: r dataframe shiny datatable conditional-formatting


    【解决方案1】:

    您的两个问题非常不同。我回复第二个。请为第一个问题打开另一个问题。

    我提出的解决方案使用 jQuery 插件 freeze Table

    library(shiny)
    
    widetbl <- t(iris[1:40,]) # a wide table for the illustration
    
    js <- HTML(paste0(c(
      '$(document).on("shiny:value", function(evt) {',
      '  if(evt.name === "wideTable") {',
      '    setTimeout(function() {',
      '      $("#wideTable").freezeTable({',
      '        fastMode: true,',
      '       columnNum: 2',
      '      });',
      '    }, 0);',
      '  }',
      '});'
    ), collapse = "\n"))
    
    ui <- fluidPage(
      tags$head(
        tags$script(
          src = "https://cdn.jsdelivr.net/gh/yidas/jquery-freeze-table/dist/js/freeze-table.min.js"
        ),
        tags$script(js)
      ),
      br(),
      tableOutput("wideTable")
    )
    
    server <- function(input, output, session){
      
      output[["wideTable"]] <- renderTable({
        widetbl
      })
      
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 2021-08-04
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多