admin管理员组文章数量:1410730
I am building a Shiny dashboard displaying a DataTable.
My objective is to have the column width of data table automatically adjusted to fit the cell values in 1 line instead of multiple lines. However, I got the following result:
It seems like Name was adjusted as what I want, but Notes did not. Then, I followed those links: .html,
Setting column width in R Shiny DataTable does not work in case of lots of column,
R Shiny set DataTable column width
Basically, the solutions suggested online were to use autoWidth = TRUE
argument with columnDefs
when rendering data table.
This did not work for me as well, below is the result:
It seems like the width of the table was narrowed paring with the initial result
Below is my final code:
header <- dashboardHeader(
title = "Test"
)
sidebar <- dashboardSidebar(
)
body <- dashboardBody(
box(title = "Test", width = 7, status = "warning", DT::dataTableOutput("df"))
)
# UI
ui <- dashboardPage(header, sidebar, body)
# Server
server <- function(input, output, session) {
output$df = DT::renderDataTable(df, options = list(
autoWidth = TRUE,
columnDefs = list(list(width = '10px', targets = c(1,3)))))
}
# Shiny dashboard
shiny::shinyApp(ui, server)
I am not sure why the width of Name column can be adjusted with the length of cell value, but that of Notes cannot. I also tried using data table in other projects, the width of the columns can only be adjusted with the length of column name.
Another option in my case would be setting the column width to be only a certain number of characters and having the full context of cell values showing up by hovering over the cell. The solution was suggested here: R Shiny Dashboard DataTable Column Width. However, this is for DT only, but it did not work out when I integrate it in Shiny.
Thanks in advance.
I am building a Shiny dashboard displaying a DataTable.
My objective is to have the column width of data table automatically adjusted to fit the cell values in 1 line instead of multiple lines. However, I got the following result:
It seems like Name was adjusted as what I want, but Notes did not. Then, I followed those links: https://rstudio.github.io/DT/options.html,
Setting column width in R Shiny DataTable does not work in case of lots of column,
R Shiny set DataTable column width
Basically, the solutions suggested online were to use autoWidth = TRUE
argument with columnDefs
when rendering data table.
This did not work for me as well, below is the result:
It seems like the width of the table was narrowed paring with the initial result
Below is my final code:
header <- dashboardHeader(
title = "Test"
)
sidebar <- dashboardSidebar(
)
body <- dashboardBody(
box(title = "Test", width = 7, status = "warning", DT::dataTableOutput("df"))
)
# UI
ui <- dashboardPage(header, sidebar, body)
# Server
server <- function(input, output, session) {
output$df = DT::renderDataTable(df, options = list(
autoWidth = TRUE,
columnDefs = list(list(width = '10px', targets = c(1,3)))))
}
# Shiny dashboard
shiny::shinyApp(ui, server)
I am not sure why the width of Name column can be adjusted with the length of cell value, but that of Notes cannot. I also tried using data table in other projects, the width of the columns can only be adjusted with the length of column name.
Another option in my case would be setting the column width to be only a certain number of characters and having the full context of cell values showing up by hovering over the cell. The solution was suggested here: R Shiny Dashboard DataTable Column Width. However, this is for DT only, but it did not work out when I integrate it in Shiny.
Thanks in advance.
Share Improve this question edited Mar 17, 2019 at 17:48 MMAASS asked Mar 17, 2019 at 15:45 MMAASSMMAASS 4331 gold badge6 silver badges20 bronze badges 1- 2 "However, this is for DT only, but it did not work out when I integrate it in Shiny." I don't think so.The answer I gave here should work in Shiny as well. – Stéphane Laurent Commented Mar 18, 2019 at 10:15
2 Answers
Reset to default 3If you don't want multiple lines, you can add the CSS property white-space: nowrap
to the cells in the columns you want.
Since you're in Shiny, it is easy to do so, by defining a CSS class and assigning it to the columns with className
in the columnDefs
option:
library(DT)
library(shiny)
dat <- data.frame(
V1 = c("A", "B"),
V2 = c(
"A cool guy living in US and Canada",
"A cool guy living in California"
),
V3 = c(
"A cool guy living in US and Canada",
"A cool guy living in California"
),
V4 = c(
"A cool guy living in US and Canada",
"A cool guy living in California"
),
V5 = c(
"A cool guy living in US and Canada",
"A cool guy living in California"
),
stringsAsFactors = FALSE
)
css <- "
.nowrap {
white-space: nowrap;
}"
ui <- fluidPage(
tags$head(
tags$style(HTML(css))
),
DTOutput("table")
)
server <- function(input, output){
output[["table"]] <- renderDT({
datatable(dat,
options = list(
columnDefs = list(
list(className = "nowrap", targets = "_all")
)
))
})
}
shinyApp(ui, server)
The problem is that the index for column widths is base 0, not 1. In the OP code, the target sets the width (to a relatively narrow 10px) for columns 2 and 4, not 1 and 3.
Try the following,
columnDefs = list(list(width = '200px', targets = c(0,2)))))
This should make the first and third columns a 200px, while all others are auto width.
本文标签: javascriptHow to set column width in R Shiny data tableStack Overflow
版权声明:本文标题:javascript - How to set column width in R Shiny data table? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744906535a2631657.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论