在Shiny中使用data.table冻结头和第一列 [英] Freezing header and first column using data.table in Shiny
问题描述
我有一个闪亮的应用程序产生一个数据表,但我不能冻结第一列和标题,所以表是很难读,因为你下来或跨。有没有反正冻结窗格?
I have a Shiny app that yields a data table, but I can't freeze the first column and the headers, so the table is hard to read as you go down or across. Is there anyway to freeze the panes? I've tried searching but have found nothing.
推荐答案
有趣的问题,现在感谢最近更新的闪光到 data.tables 1.10.2
它更容易使用各种插件和扩展。对于您的问题, FixedHeader 扩展似乎是理想的。要添加这个扩展,我们需要包括相关的 JavaScript
和 CSS
文件(参见 http://cdn.datatables.net/ ):
Interesting question and now thanks to the recent update of Shiny to data.tables 1.10.2
it is alot easier to use the various plug-ins and extensions. For your question the FixedHeader extension seems ideal. To add this extension we need to include the relevant JavaScript
and CSS
file (see http://cdn.datatables.net/):
tagList(
singleton(tags$head(tags$script(src='//cdn.datatables.net/fixedheader/2.1.2/js/dataTables.fixedHeader.min.js',type='text/javascript'))),
singleton(tags$head(tags$link(href='//cdn.datatables.net/fixedheader/2.1.2/css/dataTables.fixedHeader.css',rel='stylesheet',type='text/css')))
)
code> data.tables 有一个选项 initComplete
,它允许我们在绘制表格后规定回调。
data.tables
has an option initComplete
which allows us to stipulate a callback once table is drawn etc.
function(settings, json) {
new $.fn.dataTable.FixedHeader(this, {
left: true,
right: true
} );
}
我们将使用修改版本的 iris
数据集在末尾添加索引和一些随机数据以显示从左向右滚动:
We will use a modified version of the iris
data set adding an index and some random data at the end to show left to right scrolling:
library(shiny)
myData <- cbind(list(index = row.names(iris)), iris
, rep(list(row.names(iris)), 10))
names(myData)[7:16] <- paste0("randomData", 1:10)
runApp(
list(ui = fluidPage(
tagList(
singleton(tags$head(tags$script(src='//cdn.datatables.net/fixedheader/2.1.2/js/dataTables.fixedHeader.min.js',type='text/javascript'))),
singleton(tags$head(tags$link(href='//cdn.datatables.net/fixedheader/2.1.2/css/dataTables.fixedHeader.css',rel='stylesheet',type='text/css')))
),
dataTableOutput("mytable")
)
, server = function(input, output, session){
output$mytable <- renderDataTable(myData,
options = list(
pageLength = 50,
initComplete = I("function(settings, json){
new $.fn.dataTable.FixedHeader(this, {
left: true,
right: true
} );
}")
)
)
})
)
所以在图像中,我们可以看到我们向下滚动到记录8,通过某些方式,但标题和第一列(我们添加的索引列)仍然可见。
so in the image we can see we are scrolled down to record 8 and across some ways but the header and the first column (our added index column) are still visible.
>
这篇关于在Shiny中使用data.table冻结头和第一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!