使用R包DT使用自动宽度和scrollX进行表格渲染

问题描述 投票:0回答:1

我在使用DT包渲染表格时遇到问题。该表是根据情节图中的单击事件生成的。如果没有足够的列,我需要表格不要跨越整个宽度。因此,按照小插图,我将以下代码添加到了DT包的功能数据表中的选项中:

 autoWidth = TRUE,
 columnDefs = list(list(className = 'dt-center'
                                         , width = '200px'
                                         , targets = '_all'))

这会在第一次单击时显示所需的正确显示,如屏幕快照Auto-Click1.png中所示(请参阅第一个图像)。但是,如果我第二次单击任意点,则结果表将覆盖浏览器的整个宽度。 (请参阅Auto-Click2.png或第二张图片)。我希望表格显示为“自动点击1”,并且渲染在两次单击之间保持不变。

为了使点击之间的宽度保持不变,我尝试添加选项scrollX=TRUE。这会有所帮助,并且单击之间的宽度会保持不变。但是,列标题现在未对齐到表格其余部分的左侧。参见ScrollX-True.png(参见第3张图片)。

这里是一个完整的可复制示例:

    ---
    title: "Dashboard with DT and plotly.  Reproducible example"
    output: 
      flexdashboard::flex_dashboard:
        orientation: rows
        vertical_layout: fill
    runtime: shiny
    ---

    Dashboard
    ======================

    ```{r setup, include=FALSE}
    library(tidyverse)
    library(tidyselect)
    library(tidyr)
    library(magrittr)
    library(dplyr)
    library(ggplot2)
    library(plotly)
    library(shiny)
    library(shinydashboard)
    library(flexdashboard)
    library(rlang)
    library(DT)
    library(readxl)
    library(writexl)

    ```

    Row 
    -----------------------------------------------------------------------

    ### Plot {.no-padding}

    ```{r}
    data <- mtcars %>%
    rownames_to_column("Model") %>%
    mutate(Make = sub("\\s+.*","", Model))

    ## Plot in the top row
    renderPlotly({
      p <- data %>%
        ggplot(aes(wt, mpg, color = factor(gear))) +
        geom_point() + guides(color=guide_legend("Gear"))
      ggplotly(p,source = "A") %>%
        event_register("plotly_click")
    })
    ```



    Row
    ---------------------------------------


    ### Data Table 

    ```{r tbl}
    renderUI({
      e <- event_data("plotly_click", priority = "event", source = "A")
      chosen_gear <- data %>% 
        pull(gear) %>% 
        unique() %>% 
        sort() %>% 
        .[e[1,"curveNumber"] + 1]

      if(is.null(e))
        return("Click events will appear here")

      DF <- data %>% 
        filter(gear == chosen_gear) %>% 
        dplyr::select(Model, wt, mpg, gear, Make)

      tbl <- DF  %>%
        datatable(rownames = FALSE
                  , extensions = 'Buttons'
                  , options = list(
                    ##scrollX = TRUE, 
                    # Turning the scrollX option on causes Column headers to misalign with rest of the table
                    # Without the scrollX option table has correct width on first click but
                    # On the second click expands to the full width.
                    autoWidth = TRUE
                    , columnDefs = list(list(className = 'dt-center'
                                             , width = '200px'
                                             , targets = '_all'))
                    , dom = "Blrtip"
                    , buttons = list("copy", list(
                      extend = "collection"
                      , buttons = c("csv", "excel", "pdf")
                      , text = "Download"
                    ))
                    , lengthMenu = list(c(5, -1)
                                        , c(5, "All"))
                    , pageLength = 5))

      output$table <- renderDT({tbl})

      DTOutput("table")
    })

Auto-Click1

Auto-Click2

ScrollX-True

感谢您的帮助或指导。

r plotly shinydashboard dt flexdashboard
1个回答
0
投票

正如https://github.com/rstudio/DT/issues/752#issuecomment-575706297中的回答,我想到的最简单的方法是将最后一行从DTOutput('tbl')更改为]

div(style = 'width:500px;margin:auto', DTOutput("table"))
© www.soinside.com 2019 - 2024. All rights reserved.