无法隐藏列并在 Shiny DT 中设置行名 = FALSE
Can't hide columns AND set rownames = FALSE in Shiny DT
我在 Shiny 中创建了一个数据 table,它使用 DT 根据一组隐藏列中的值设置值的样式。 table 显示公司的单位是否达到了他们的电话和电子邮件目标。
问题是,当我隐藏列(使用 columnDefs = list(list(targets = c(4, 5), visible = FALSE))
)时,我无法再在 datatable()
调用下使用 rownames = FALSE
:table 显示为没有数据。有谁知道我怎样才能让这两个选项一起工作?
我用过以下文章:
https://rstudio.github.io/DT/010-style.html
library(shiny)
library(tidyverse)
library(DT)
x <- tibble(
Unit = c("Sales", "Marketing", "HR"),
Calls = c(100, 150, 120),
Emails = c(200, 220, 230),
Calls_goal = c(1, 0, 0),
Emails_goal = c(0, 1, 1)
)
ui <- fluidPage(
mainPanel(
DT::dataTableOutput("table")
)
)
server <- function(input, output) {
output$table <- DT::renderDataTable({
# Can't use both visible = FALSE and rownames = FALSE
datatable(x,
options = list(
columnDefs = list(list(targets = c(4, 5), visible = FALSE)) # THIS
),
rownames = TRUE) %>% # OR THIS
formatStyle(
columns = c('Calls', 'Emails'),
valueColumns = c('Calls_goal', 'Emails_goal'),
color = styleEqual(c(1, 0), c("red", "black"))
)
})
}
shinyApp(ui = ui, server = server)
由于行名也是一列,当您将它们设置为 false 时,您需要重新索引要隐藏的列。因此,在您的特定情况下,第 5 列不再存在。现在是第 4 个,第 4 个是第 3 个,所以你的代码应该是这样的:
server <- function(input, output) {
output$table <- DT::renderDataTable({
# Can't use both visible = FALSE and rownames = FALSE
datatable(x, rownames=F,
options = list(
columnDefs = list(list(targets = c(3, 4), visible = FALSE) # THIS
)
)) %>% # OR THIS
formatStyle(
columns = c('Calls', 'Emails'),
valueColumns = c('Calls_goal', 'Emails_goal'),
color = styleEqual(c(1, 0), c("red", "black"))
)
})
}
我在 Shiny 中创建了一个数据 table,它使用 DT 根据一组隐藏列中的值设置值的样式。 table 显示公司的单位是否达到了他们的电话和电子邮件目标。
问题是,当我隐藏列(使用 columnDefs = list(list(targets = c(4, 5), visible = FALSE))
)时,我无法再在 datatable()
调用下使用 rownames = FALSE
:table 显示为没有数据。有谁知道我怎样才能让这两个选项一起工作?
我用过以下文章:
https://rstudio.github.io/DT/010-style.html
library(shiny)
library(tidyverse)
library(DT)
x <- tibble(
Unit = c("Sales", "Marketing", "HR"),
Calls = c(100, 150, 120),
Emails = c(200, 220, 230),
Calls_goal = c(1, 0, 0),
Emails_goal = c(0, 1, 1)
)
ui <- fluidPage(
mainPanel(
DT::dataTableOutput("table")
)
)
server <- function(input, output) {
output$table <- DT::renderDataTable({
# Can't use both visible = FALSE and rownames = FALSE
datatable(x,
options = list(
columnDefs = list(list(targets = c(4, 5), visible = FALSE)) # THIS
),
rownames = TRUE) %>% # OR THIS
formatStyle(
columns = c('Calls', 'Emails'),
valueColumns = c('Calls_goal', 'Emails_goal'),
color = styleEqual(c(1, 0), c("red", "black"))
)
})
}
shinyApp(ui = ui, server = server)
由于行名也是一列,当您将它们设置为 false 时,您需要重新索引要隐藏的列。因此,在您的特定情况下,第 5 列不再存在。现在是第 4 个,第 4 个是第 3 个,所以你的代码应该是这样的:
server <- function(input, output) {
output$table <- DT::renderDataTable({
# Can't use both visible = FALSE and rownames = FALSE
datatable(x, rownames=F,
options = list(
columnDefs = list(list(targets = c(3, 4), visible = FALSE) # THIS
)
)) %>% # OR THIS
formatStyle(
columns = c('Calls', 'Emails'),
valueColumns = c('Calls_goal', 'Emails_goal'),
color = styleEqual(c(1, 0), c("red", "black"))
)
})
}