R Shiny renderTable - 冻结列
R Shiny renderTable - freeze columns
寻求一些帮助将冻结帧添加到 R Shiny 中的 renderTable
。我使用 renderTable
而不是 DT
包 renderDataTable
因为我有一个超过 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)
}
我正在寻找冻结前两列的功能,fruit 和 num。
你的两个问题很不一样。我回复第二个。第一个问题请另开一个问题。
我提出的解决方案利用了 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)
寻求一些帮助将冻结帧添加到 R Shiny 中的 renderTable
。我使用 renderTable
而不是 DT
包 renderDataTable
因为我有一个超过 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)
}
我正在寻找冻结前两列的功能,fruit 和 num。
你的两个问题很不一样。我回复第二个。第一个问题请另开一个问题。
我提出的解决方案利用了 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)