I am trying to build a shiny app that uses the datatable FixedColumns plugin:
https://datatables.net/extensions/fixedcolumns/
The datasest I am using will have around 100 columns and I want to fix the first five columns and allow the user to scroll through the rest. From the examples it looks like I'd need to use this javascript:
https://datatables.net/release-datatables/extensions/FixedColumns/examples/two_columns.html
$(document).ready(function() {
var table = $('#example').DataTable( {
scrollY: "300px",
scrollX: true,
scrollCollapse: true,
paging: false
} );
new $.fn.dataTable.FixedColumns( table, {
leftColumns: 2
} );
} );
I don't know javascript but in the past I have been able to use I() to insert javascript options. This time though it looks like I need to do something else. I've tried the code below and get the message: "ERROR: 'options' must be a named list'.
library(shiny)
library(ggplot2)
data(diamonds)
hw <- diamonds
runApp(
list(ui=(
fluidPage(
tabsetPanel(
id = 'dataset',
tabPanel('hw', dataTableOutput('mytable1'))
))),
server = (function(input, output, session) {
output$mytable1 <- renderDataTable(
head(hw, 50),
options = list(scrollY = '300px',
scrollX = TRUE,
scrollCollapse = TRUE,
paging = FALSE,
I("new $.fn.dataTable.FixedColumns( table, {
leftColumns: 5
} );")
))
})
))
In case anyone comes across this question more recently, you can now use the FixedColumns extension directly without any javascript:
https://rstudio.github.io/DT/extensions.html
m = as.data.frame(round(matrix(rnorm(100), 5), 5))
datatable(
m, extensions = 'FixedColumns',
options = list(
dom = 't',
scrollX = TRUE,
fixedColumns = TRUE
)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With