R Shiny DT 数据 table 列宽适用于所有列,但不适用于特定列
R Shiny DT data table column width works on ALL columns, but not on specific column
我查看了文档、示例和其他答案。但是,就我的生活而言,我无法让 DT::datatable()
加宽输出中的一列。当我设置包含 _all
列的选项时,它起作用了,但显然不是我想要的。
这是一个工作示例:
library(dplyr)
library(shiny)
library(DT)
library(data.table)
mtcars <- mtcars[1:5, ]
ui <- fluidPage(
fluidRow(
dataTableOutput(('mtcarsDT')),
)
)
server <- function(input, output, session) {
output$mtcarsDT <- DT::renderDataTable({
recFeedbackCol <- lapply(1:nrow(mtcars), function(recnum)
as.character(
radioButtons(
paste0(
'rec', recnum),
'',
choices = c('good' = 'Good', 'bad' = 'Bad', 'neutral' = 'Neutral'),
inline = TRUE
)
)
)
recFeedbackCol <- tibble(feedback = recFeedbackCol)
mtcars <- bind_cols(
mtcars,
recFeedbackCol
)
mtcars %>%
DT::datatable(
escape = FALSE, selection = 'none',
extensions = 'FixedColumns',
options = list(
paging = FALSE, pageLength = 10, ordering = FALSE, scrollX = TRUE,
fixedColumns = list(leftColumns = 2),
preDrawCallback = JS(
'function() { Shiny.unbindAll(this.api().table().node()); }'
),
drawCallback = JS(
'function() { Shiny.bindAll(this.api().table().node()); } '
),
autoWidth = TRUE,
columnDefs = list(list(width = '200px', targets = 'feedback'))
)
)
})
}
shinyApp(ui = ui, server = server)
更改 targets = '_all'
有效。但是,这会加宽所有列。
> packageVersion('shiny')
[1] ‘1.4.0’
> packageVersion('DT')
[1] ‘0.17’
我遗漏了什么吗?
更新:
现在,我正在使用 ncol(mtcars)
并带有一些选项,但 DT 根本不呈现。我得到列,显示零行:
library(dplyr)
library(shiny)
library(DT)
library(data.table)
mtcars <- mtcars[1:5, ]
ui <- fluidPage(
fluidRow(
dataTableOutput(('mtcarsDT')),
)
)
server <- function(input, output, session) {
output$mtcarsDT <- DT::renderDataTable({
recFeedbackCol <- lapply(1:nrow(mtcars), function(recnum)
as.character(
radioButtons(
paste0(
'rec', recnum),
'',
choices = c('good' = 'Good', 'bad' = 'Bad', 'neutral' = 'Neutral'),
inline = TRUE
)
)
)
recFeedbackCol <- tibble(feedback = recFeedbackCol)
mtcars <- bind_cols(
mtcars,
recFeedbackCol
)
mtcars %>%
DT::datatable(
extensions = 'FixedColumns',
rownames = FALSE,
escape = FALSE,
class="compact cell-border",
options = list(
pageLength = 15,
lengthChange = FALSE,
scrollX = TRUE,
searching = FALSE,
dom = 't',
ordering = TRUE,
fixedColumns = list(leftColumns = 2),
preDrawCallback = JS(
'function() { Shiny.unbindAll(this.api().table().node()); }'
),
drawCallback = JS(
'function() { Shiny.bindAll(this.api().table().node()); } '
),
autoWidth = TRUE,
columnDefs = list(
list(width = '200px', targets = ncol(mtcars))
)
)
)
})
}
shinyApp(ui = ui, server = server)
对于targets
,您可以使用列索引号(索引从零开始):
targets = 3
或者要定位多个特定列,请使用数组:
targets = list(3,4)
还有一些其他选项 - 请参阅 here 以供参考。
更新
因此,使用上面提到的关于索引是从零开始的事实,并查看参考文档(参见上面的link),我们可以说以下内容:
- table 中的第一列的索引为 0。
- table 中的第二列的索引为 1。
- ...等等。
我们也可以说:
- table 中的最后一列的索引为 -1。
- table 中倒数第二列的索引为 -2。
- ...等等。
我查看了文档、示例和其他答案。但是,就我的生活而言,我无法让 DT::datatable()
加宽输出中的一列。当我设置包含 _all
列的选项时,它起作用了,但显然不是我想要的。
这是一个工作示例:
library(dplyr)
library(shiny)
library(DT)
library(data.table)
mtcars <- mtcars[1:5, ]
ui <- fluidPage(
fluidRow(
dataTableOutput(('mtcarsDT')),
)
)
server <- function(input, output, session) {
output$mtcarsDT <- DT::renderDataTable({
recFeedbackCol <- lapply(1:nrow(mtcars), function(recnum)
as.character(
radioButtons(
paste0(
'rec', recnum),
'',
choices = c('good' = 'Good', 'bad' = 'Bad', 'neutral' = 'Neutral'),
inline = TRUE
)
)
)
recFeedbackCol <- tibble(feedback = recFeedbackCol)
mtcars <- bind_cols(
mtcars,
recFeedbackCol
)
mtcars %>%
DT::datatable(
escape = FALSE, selection = 'none',
extensions = 'FixedColumns',
options = list(
paging = FALSE, pageLength = 10, ordering = FALSE, scrollX = TRUE,
fixedColumns = list(leftColumns = 2),
preDrawCallback = JS(
'function() { Shiny.unbindAll(this.api().table().node()); }'
),
drawCallback = JS(
'function() { Shiny.bindAll(this.api().table().node()); } '
),
autoWidth = TRUE,
columnDefs = list(list(width = '200px', targets = 'feedback'))
)
)
})
}
shinyApp(ui = ui, server = server)
更改 targets = '_all'
有效。但是,这会加宽所有列。
> packageVersion('shiny')
[1] ‘1.4.0’
> packageVersion('DT')
[1] ‘0.17’
我遗漏了什么吗?
更新:
现在,我正在使用 ncol(mtcars)
并带有一些选项,但 DT 根本不呈现。我得到列,显示零行:
library(dplyr)
library(shiny)
library(DT)
library(data.table)
mtcars <- mtcars[1:5, ]
ui <- fluidPage(
fluidRow(
dataTableOutput(('mtcarsDT')),
)
)
server <- function(input, output, session) {
output$mtcarsDT <- DT::renderDataTable({
recFeedbackCol <- lapply(1:nrow(mtcars), function(recnum)
as.character(
radioButtons(
paste0(
'rec', recnum),
'',
choices = c('good' = 'Good', 'bad' = 'Bad', 'neutral' = 'Neutral'),
inline = TRUE
)
)
)
recFeedbackCol <- tibble(feedback = recFeedbackCol)
mtcars <- bind_cols(
mtcars,
recFeedbackCol
)
mtcars %>%
DT::datatable(
extensions = 'FixedColumns',
rownames = FALSE,
escape = FALSE,
class="compact cell-border",
options = list(
pageLength = 15,
lengthChange = FALSE,
scrollX = TRUE,
searching = FALSE,
dom = 't',
ordering = TRUE,
fixedColumns = list(leftColumns = 2),
preDrawCallback = JS(
'function() { Shiny.unbindAll(this.api().table().node()); }'
),
drawCallback = JS(
'function() { Shiny.bindAll(this.api().table().node()); } '
),
autoWidth = TRUE,
columnDefs = list(
list(width = '200px', targets = ncol(mtcars))
)
)
)
})
}
shinyApp(ui = ui, server = server)
对于targets
,您可以使用列索引号(索引从零开始):
targets = 3
或者要定位多个特定列,请使用数组:
targets = list(3,4)
还有一些其他选项 - 请参阅 here 以供参考。
更新
因此,使用上面提到的关于索引是从零开始的事实,并查看参考文档(参见上面的link),我们可以说以下内容:
- table 中的第一列的索引为 0。
- table 中的第二列的索引为 1。
- ...等等。
我们也可以说:
- table 中的最后一列的索引为 -1。
- table 中倒数第二列的索引为 -2。
- ...等等。