在Shiny dataTableOutput中控制表格宽度

Col*_*lin 13 r shiny

我无法使用该函数控制我添加到闪亮应用程序的数据表的宽度dataTableOutput().我试图width在函数中使用参数,但它在输出中没有任何改变,并且没有错误〜它没有告诉我它忽略了width参数.

library(shiny)
library(shinythemes)

ui <- fluidPage(theme = shinytheme("Spacelab"),
            fluidRow(
              column(6,dataTableOutput(outputId = "table")),
              column(6,p(textOutput("para")))
  )
)

server <- function(input, output){

  df <- as.data.frame(matrix(0, ncol = 15, nrow = 20))

  output$table <- renderDataTable({df})

  output$para <- renderText({
    text <- rep(x = "Hello World",1000)
  })
}
shinyApp(ui = ui,server = server)
Run Code Online (Sandbox Code Playgroud)

CSJ*_*ell 24

dataTableOutput没有参数宽度.您可以columnfluidRowwith参数宽度内使用,提供1到12之间的整数.

library(shinythemes)
ui <- fluidPage(theme = shinytheme("Spacelab"),
    fluidRow(
        column(
            dataTableOutput(outputId = "table"), width = 6)
    )
)

server <- function(input, output){
    df <- as.data.frame(matrix(0, ncol = 20, nrow = 5))
    output$table <- renderDataTable({df}, 
        options = list(scrollX = TRUE))
}
shinyApp(ui = ui,server = server)
Run Code Online (Sandbox Code Playgroud)

JavaScript库DataTable中的选项可以通过renderDataTable参数选项直接传递.例如,将scrollX设置为true允许表滚动.

  • 在您的原始示例中,您有大量列。Shiny 仍在呈现网页,因此如果列太宽而无法放入窗格的子集,则表格将使用页面的可用部分显示。 (3认同)
  • 我尝试使用列并相应地更新了我的示例,似乎 `dataTableOutput` 不想合作 (2认同)
  • @Collin 我已更新示例以演示为表格添加滚动条...您需要将 JavaScript 参数作为列表传递给 `renderDataTable`。 (2认同)

小智 7

如果您使用"DT"R包和相应的DT::dataTableOutputDT::renderDataTable,您可以使用"宽度"选项与这些调用,显然可以是%(例如宽度="100%")或像素(宽度= 300)哪个应该让你得到你想要的控制权.

请参阅:https: //rstudio.github.io/DT/shiny.html

从该页面注意:

重要:确保在调用dataTableOutput和renderDataTable时使用DT ::前缀,以便保证调用这些函数的DT版本,而不是被弃用的Shiny版本.如果确保库(光泽)之后的库(DT),通常DT版本应该覆盖闪亮版本,如果你不使用DT ::前缀(如果有疑问,请使用此前缀,直到我们完全删除这些函数来自闪亮)