R Shiny DT data table column width works on ALL columns, but not on specific column
For targets
you can use a column index number (indexing is zero-based):
targets = 3
Or to target multiple specific columns, use an array:
targets = list(3,4)
There are some additional options, too - see here for reference.
Update
So, using the fact mentioned above about indexing being zero-based, and looking at the reference documentation (see the above link), we can say the following:
- The first column in the table has an index of 0.
- The second column in the table has an index of 1.
- ... and so on.
And we can also say:
- The last column in the table has an index of -1.
- The second-to-last column in the table has an index of -2.
- ... and so on.
Set the width of datatable or the width of its columns in a shiny app
You could use splitLayout
instead of column
:
library(shiny)
library(DT)
library(dplyr)
dat <- structure(list(Population = c("p1", "p1", "p1", "p1", "p1", "p1", "p1",
"p1", "p1", "p1", "p2", "p2", "p2", "p2", "p2", "p2"), var1 = c("p1_1_a",
"p1_1_b", "p1_2_a", "p1_2_b", "p1_3_a", "p1_3_b", "p1_4_a", "p1_4_b",
"p1_5_a", "p1_5_b", "p2_a", "p2_b", "p2_c", "p2_d", "p2_e", "p2_f"),
Population2 = c("p1", "p1", "p1", "p1", "p1", "p1", "p1", "p1", "p1", "p1",
"p2", "p2", "p2", "p2", "p2", "p2"), var12 = c("p1_1_a", "p1_1_b", "p1_2_a",
"p1_2_b", "p1_3_a", "p1_3_b", "p1_4_a", "p1_4_b", "p1_5_a", "p1_5_b", "p2_a",
"p2_b", "p2_c", "p2_d", "p2_e", "p2_f"),U = c(1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1,
1, 0, 1, 1, 0), D = c(25, 12, 14, 11, 3, 3, 2, 4, 5, 0, 0, 0, 0, 25, 12, 14),
Udad = c(1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0), Ddshgg = c(25, 12,
14, 11, 3, 3, 2, 4, 5, 0, 0, 0, 0, 25, 12, 14)), row.names = c(NA, -16L),
class = c("tbl_df", "tbl", "data.frame"))
ui <- fluidPage(fluidRow(
splitLayout(
style = "margin-right: 0px;",
DTOutput("table", width = "100%"),
DTOutput("table2", width = "100%"),
DTOutput("table3", width = "100%"),
cellWidths = c("630px", "630px", "900px"),
cellArgs = list(style = "margin-right: 15px;")
)
))
server <- function(input, output) {
output[["table"]] <- renderDT({
dtable <- datatable(
dat,
rownames = FALSE,
options = list(
scrollX = FALSE,
order = list(list(1, 'asc')),
pageLength = 16 # ascending order
)
)
dtable
})
output[["table2"]] <- renderDT({
dtable <- datatable(
dat,
rownames = FALSE,
options = list(
scrollX = FALSE,
order = list(list(1, 'asc')),
pageLength = 16 # ascending order
)
)
dtable
})
output[["table3"]] <- renderDT({
dat$Comparison1 <- NA
dat$cOMPARISON2 <- NA
dtable <- datatable(
dat,
rownames = FALSE,
options = list(
scrollX = FALSE,
order = list(list(1, 'asc')),
pageLength = 16 # ascending order
)
)
dtable
})
}
shinyApp(ui, server)
Shiny: Unable to set column width in Shiny DataTables
You need to add scrollX=TRUE
in the options as per https://github.com/rstudio/DT/issues/29
Related Topics
Data.Table and Parallel Computing
How to Directly Select the Same Column from All Nested Lists Within a List
Duplicate 'Row.Names' Are Not Allowed Error
Conditional Coloring of Cells in Table
Variable Name Restrictions in R
Shiny Slider on Logarithmic Scale
Is There a Way of Manipulating Ggplot Scale Breaks and Labels
Pretty Ticks for Log Normal Scale Using Ggplot2 (Dynamic Not Manual)
Displaying a Greater Than or Equal Sign
Subset Based on Variable Column Name
Reading 40 Gb CSV File into R Using Bigmemory
Count Number of Rows Matching a Criteria