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
How to adjust width of one column for shiny DataTables created with the JavaScript?
DataTables are not very nice, when it comes to column widths. It sais here that widths will never be taken literally from the given definition, but always adapted to the table size and what not. Thats why your efforts were in vain.
But you can still shape things to your need. At first, using checkboxInput
creates a container around the checkbox that has a default width of 300 pixels, which are the main reason for the column to be so big. You could in a first step unset this width to see what "natural" size the column would have.
If you want to reduce the size even more, a css rule for the column's width is working fine. For that and the above part, we equip the first column cells with a specific class name.
Weave
columnDefs = list(list(targets = 0, className = "small" ))
into your DataTable definition and then add
td.small .shiny-input-container{width:auto;}
to your css to unset the predefined width in this column.
Further minifications can be achieved by the css rule
td.small{width:30px;}
Change column width in datatable (with URLs in one column or without)
You can create a HTML link instead of just showing the URL as a raw text, if you use datatable(escape = FALSE)
:
library(data.table)
library(DT)
var1 <- rep(seq(as.Date("2020-01-01"), as.Date("2020-01-10"), by = "days"), each = 5)
var2 <- rep(c("a", "b", "c", "d", "e"), each = 10)
var3 <- c(rep(NA, 10), "This is a non-empty entry.", rep(NA, 5), "yet another text. oh wow, this one is long. in the real world, there are even longer texts,", rep(NA, 2), "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "Every 60 seconds in Africa a minute passes", rep(NA, 20), "some more dummy text", rep(NA, 8))
var4 <- c(rep(NA, 10), "https://stackoverflow.com/questions/55208855/how-to-set-column-width-in-r-shiny-data-table", rep(NA, 5), "https://stackoverflow.com/questions/25205410/r-shiny-set-datatable-column-width?noredirect=1&lq=1", rep(NA, 2), "https://www.youtube.com/watch?v=sAn7baRbhx4", "https://www.timeanddate.com/weather/antarctica", rep(NA, 20), "https://www.theguardian.com/football/live/2022/feb/06/senegal-v-egypt-africa-cup-of-nations-final-live-score-updates", rep(NA, 8))
var5 <- c(1:50)
var6 <- c(21:70)
df <- data.table(var1, var2, var3, var4, var5, var6)
# Transform text to HTML
df$var4 <- sapply(df$var4, function(x) paste0("<a href=\"", x, '\">link</a>'))
datatable(
escape = FALSE,
df[var3 != "NA", c("var1", "var2", "var3", "var4", "var6")],
options = list(
scrollX = TRUE,
autoWidth = TRUE,
columnDefs = list((list(targets = c(1, 2), visible = TRUE, width = "5%")), (list(targets = c(3), visible = TRUE, width = "80%")), (list(targets = c(4, 5), visible = TRUE, width = "5%")))
)
)
Related Topics
Generate Random Integers Between Two Values with a Given Probability Using R
Shiny Dashboard Mainpanel Height Issue
Plot Multiple Datasets with Ggplot
Alignment of Numbers on the Individual Bars with Ggplot2
How to Ensure That a Partition Has Representative Observations from Each Level of a Factor
How Does R's Ifelse Work with Character Data
Group_By() into Fill() Not Working as Expected
Retain Attributes When Using Gather from Tidyr (Attributes Are Not Identical)
Getting File Path from Shiny UI (Not Just Directory) Using Browse Button Without Uploading the File
Package Domc Not Available for R Version 3.0.0 Warning in Install.Packages
Find Elements Not in Smaller Character Vector List But in Big List
Converting a Data.Frame to a List of Lists
Downloading Files from Ftp with R
List and Description of All Packages in Cran from Within R