how to remove padding in datatable column - r

Is there any way of setting the datatable options to reduce the column padding? This link suggested using autoWidth=TRUE along with scrollX=TRUE, but it doesn't work in my code.
As you can see in the image below there is a big gap between columns forcing the user to scroll across, which I'd prefer to avoid if possible. This link and this one have the same problem in java
Here's the code for rendering the datatable.
output$book_table <- DT::renderDT(RVTables$book %>%
filter(deal==as.numeric(input$deal_choice)),
selection = list(mode="single",selected=row_edited),
editable = TRUE,
rownames = FALSE,
options=list(
autoWidth=TRUE,
scrollX = TRUE,
ordering=FALSE,
pageLength=12,
scrollY = TRUE,
bLengthChange= FALSE,
searching=FALSE
)
)
Thanks for any help.

After some google searching I found the line of code class="compact cell-border", which reduces the padding around column headers. Here is my code to render the table in case it helps anyone else.
output$book_table <- DT::renderDataTable({
DT::datatable(
deal_reactive(),
editable = TRUE,
rownames = FALSE,
class="compact cell-border",
selection = list(mode = "single",
target = "row",
selected = previous_row),
options = list(
dom="t",
autoWidth=TRUE,
scrollX = TRUE,
ordering=FALSE,
pageLength = 28,
bLengthChange= FALSE,
displayStart = previous_page,
searching=FALSE
)
)
})

Related

Datatables Buttons to show/hide RowGroups

I have a table that uses RowGroups and Buttons/Colvis.
Much like the buttons for Colvis, I'd like to have buttons to show only specific RowGroups. Currently I have a long table with quite a few RowGroups. Often users will want to see only specific column groups and I'd like to prevent them from having to scroll down all the way constantly.
From the examples below, imagine there being a button "Row group" visibility, exactly like the Column visiblity in Colvis.
What I described can be accomplished using the SearchPanes extension. You can select your searchPanes targets on the same columns as your rowGroup targets.
You can also set your searchPanes to be a button.
Example from my code, with use in renderDT:
output$tbl <- renderDT({
datatable(data,
rownames = FALSE,
caption = 'Some caption',
extensions = c('ColReorder', 'RowGroup', "Buttons",
"Select", "SearchPanes"),
options = list(#DOM options
dom = "Bfrtip",
#Rowgroup options
rowGroup = list(dataSrc = c(0,2)),
#Buttons options
stateSave = TRUE,
buttons = c('colvis', 'csv', 'excel', 'searchPanes'),
#ColReorder options
colReorder = TRUE,
#SearchPanes options
columnDefs = list(
list(searchPanes = list(show = FALSE), targets = 1:7)),
selection = 'none',
#Overige options
paging = FALSE)
)}, server = FALSE)
Also credit to Matt Herman's blog post: https://mattherman.info/blog/dt_searchpanes/

Set column width while using scrollX in R

I'm trying to change the width of some columns in a DT::datatable, unfortunately using the columnDefs option only appears to work when there are a small number of columns in the data.
When I add all the columns to my data the column widths no longer follow what I put in the columnDefs options.
Here is an example, as you can see the 1st table the width are all constant, whereas in the second table I have been able to manual set the widths as I desire. Removing the scrollX argument doesn't work either, and given the number of columns my data has I need it in there.
library(MASS)
library(shiny)
library(DT)
ui <- fluidPage(
mainPanel(
DT::dataTableOutput("table1"),
br(),
br(),
br(),
DT::dataTableOutput("table2")
)
)
server <- function(input, output) {
output$table1 <- DT::renderDataTable({
DT::datatable(
Cars93[,-(20:27)],
rownames = FALSE,
options = list(
pageLength = 5,
autowidth = TRUE,
scrollX = TRUE,
searching = TRUE,
ordering = TRUE,
paging = TRUE,
columnDefs = list(list(width = "200px", targets = c(0:2)),
list(width = "20px", targets = 3),
list(width = "50px", targets = 4))
)
)
})
output$table2 <- DT::renderDataTable({
DT::datatable(
Cars93[,-(6:27)],
rownames = FALSE,
options = list(
pageLength = 5,
autowidth = TRUE,
scrollX = TRUE,
searching = TRUE,
ordering = TRUE,
paging = TRUE,
columnDefs = list(list(width = "200px", targets = c(0:2)),
list(width = "20px", targets = 3),
list(width = "50px", targets = 4))
)
)
})
}
shinyApp(ui, server)
What do I need to change in my code to be able to set the column widths in the 1st table like I have them in the 2nd table while will having all the columns and scrollX?
Thanks
Try:
ui <- fluidPage(
tags$head(
tags$style(
HTML("table {table-layout: fixed;}")
)
),
......
and replace autowidth with autoWidth.

Hide a column in shiny datatable but keep it searchable

The DT package in Shiny produces a table with a searchbar that searches over every column in the table. I have a column of metadata which I do not want to display in the table, but I still want the rows to come up if I search with the search bar.
For example, the app below contains a column titled searchCol . This column is just letters. I want to hide this column in the actual table, and I want to be able to search for the letter b , using the DT search bar, and have the second row show up.
Is there a way to hide the column but have it still work with the search bar?
library(shiny)
library(DT)
ui <- fluidPage(
DTOutput('tbl1'),
)
server <- function(input, output, session) {
output$tbl1 <- DT::renderDT(server = TRUE, {
datatable(
cbind(data.frame(replicate(3,sample(0:1,26,rep=TRUE))), data.frame(searchCol = letters)),
escape = FALSE,
rownames = FALSE,
filter = list(position = "top", clear = FALSE, plain = TRUE),
selection = "single",
options = list(
autoWidth = TRUE,
pageLength = 50,
lengthMenu = c(50, 100, 1000),
dom = 'Blfrtip',
buttons = c('copy', 'excel')
)
)
})
}
shinyApp(ui, server)
I've adapted the answer from here to the format you need to use in DT::datatable. You can use columnDefs to define the render options for the different columns, targets defines which column you mean. Please note that the JS library datatables starts counting columns at 0.
library(shiny)
library(DT)
ui <- fluidPage(
DTOutput('tbl1'),
)
server <- function(input, output, session) {
output$tbl1 <- DT::renderDT(server = TRUE, {
datatable(
cbind(data.frame(replicate(3,sample(0:1,26,rep=TRUE))), data.frame(searchCol = letters)),
escape = FALSE,
rownames = FALSE,
filter = list(position = "top", clear = FALSE, plain = TRUE),
selection = "single",
options = list(
autoWidth = TRUE,
pageLength = 50,
lengthMenu = c(50, 100, 1000),
dom = 'Blfrtip',
buttons = c('copy', 'excel'),
columnDefs = list(
list(
targets = 3,
searchable = TRUE,
visible = FALSE
)
)
)
)
})
}
shinyApp(ui, server)

Add download buttons in DT::renderDataTable

I am trying to add download buttons ('copy', 'csv', 'excel', 'pdf') above the table in my R Shiny app, but the renderDataTable seems doesn't work when using a datatable inside.
output$mytable1 <- DT::renderDataTable(
datatable(
{ plots.dfs()[[1]] },
rownames = TRUE,
options = list(
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = FALSE,
dom = 'tB',
buttons = c('copy', 'csv', 'excel', 'pdf')
),
class = "display"
))
When I use DT::renderDataTable without DT::datatable inside, renderDataTable works well and I have all features (filters, search field, etc), except download buttons (what I am trying to add)
output$mytable1 = DT::renderDataTable({ plots.dfs()[[1]] })
Do you have any idea of what I am doing wrong? Thanks for your help
As Stephan said in comment, the way to add buttons is the following:
output$mytable1 <- DT::renderDataTable(
DT::datatable(
{ plots.dfs()[[1]] },
extensions = 'Buttons',
options = list(
paging = TRUE,
searching = TRUE,
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'tB',
buttons = c('copy', 'csv', 'excel')
),
class = "display"
))
Adding one more solution to keep search and paging (dom = 'Bfrtip'):
datatable(data, extensions = "Buttons",
options = list(paging = TRUE,
scrollX=TRUE,
searching = TRUE,
ordering = TRUE,
dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf'),
pageLength=5,
lengthMenu=c(3,5,10) ))
Adding an answer that is more explicit about allowing to download the whole table since it should be more clearly outlined in my opinion. Using renderDT({}) the download buttons only download the data currently being displayed. You can make the buttons download the entire dataset with renderDT(server = FALSE, {}) as used below:
renderDT(server=FALSE,{
# Load data
data <- mtcars
# Show data
datatable(data, extensions = 'Buttons',
options = list(scrollX=TRUE, lengthMenu = c(5,10,15),
paging = TRUE, searching = TRUE,
fixedColumns = TRUE, autoWidth = TRUE,
ordering = TRUE, dom = 'tB',
buttons = c('copy', 'csv', 'excel','pdf')))
})

R Shiny: How to add pagination in DT::renderDataTable

I am trying to add pagination, search box and selector in my R Shiny app, but it doesn't work for now (I tried paging = TRUE and searching = TRUE, in options as you can see bellow but it doesn't work). Do you have any idea of what I should add?
output$mytable1 <- DT::renderDataTable(
DT::datatable(
{ plots.dfs()[[1]] },
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
'Table 2: ', htmltools::em('This is a simple caption for the table.')
),
extensions = 'Buttons',
options = list(
paging = TRUE,
searching = TRUE,
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'tB',
buttons = c('copy', 'csv', 'excel')
),
class = "display"
))
I have added a screenshot of table I have now, and the expected table.
Thanks for your help]1
You can modify the dom parameter, for example as follows:
DT::datatable(
{ mtcars },
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
'Table 2: ', htmltools::em('This is a simple caption for the table.')
),
extensions = 'Buttons',
options = list(
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'Bftsp',
buttons = c('copy', 'csv', 'excel')
))
To add page length, also add l to the string. Hope this helps!

Resources