如何从R Shiny中的数据表中删除第一列(索引)

时间:2019-03-18 20:42:08

标签: html css r shiny dt

我想知道是否有一种方法可以从Shiny的数据表中删除索引列(第一列)。

例如,在名称列之前的(1、2、3)列,如以下屏幕截图所示:

enter image description here

下面是我的代码:

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)

谢谢。

1 个答案:

答案 0 :(得分:1)

https://rstudio.github.io/DT/上有一些关于该软件包的出色文档,我强烈建议您通读。

无论如何,请使用rownames = FALSE软件包提供的DT参数,如下所示:

library(shinydashboard)
library(DT)

df <- mtcars

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, rownames = FALSE,
                                  options = list(
                                    autoWidth = TRUE,
                                    columnDefs = list(list(width = '10px', targets = c(1,3)))))
}

# Shiny dashboard
shiny::shinyApp(ui, server)