Shiny数据表的列标题被移位

时间:2017-09-18 20:48:43

标签: shiny dt

当我运行我的Shiny应用程序时,我的数据表的标题会向左移动。请参阅下文。enter image description here请将此表格放在标签A上。

当我单击其他选项卡(选项卡B)时,标题会正确对齐,然后再次单击选项卡A.请参阅下面的更正标题。

enter image description here

知道是什么原因引起的吗?下面是我的代码的简化版本。提前谢谢!

ui.R

    library("shinythemes")

    fluidPage(title = "Segmentation App", theme = shinytheme("spacelab"),

        navbarPage("Segmentation", id = "allResults",
            tabPanel(value='result_scorecard', title='ScoreCard', 
                     sidebarLayout(
                       sidebarPanel(
                         h4("Select a cluster solution to profile"),
                         width = 3
                       ),

                       mainPanel(
                         verticalLayout(
                           helpText(strong('Summary of Cluster Solutions')),
                           column(DT::dataTableOutput('out_best'), width = 12),                              
                           helpText(strong('ScoreCard Table')),
                           column(DT::dataTableOutput('out_scorecard'), width = 12)

                         )
                       )
                     )
            ),

            tabPanel(value='profile', title='Profile', 
                     verticalLayout(
                         column(DT::dataTableOutput('prop_by_cluster_ind'), width=10)
                      )
            )
        )
    )

server.R

function(input, output, session) {

    best_sols <- reactive({
     A <- c(100, 101, 201)
     B <- c(100, 101, 201)
     C <- c(100, 101, 201)
     temp <- as.matrix(cbind(A, B, C))
     colnames(temp) <- c("A", "B", "C")
     rownames(temp) <- c("k=1","k=2","k=3")
     return(temp)
    })

    score_seg <- reactive({
      A <- c("solution=1","solution=2","solution=3","solution=4","solution=5")
      B <- c(100, 101, 201, 333, 444)
      C <- c(100, 101, 201, 333, 444)
      temp <- data.frame(A, B, C)
      colnames(temp) <- c("A", "B", "score_seg")
      return(temp)

    })

    profile_result_ind <- reactive({
      A1 <- c("var1","var2","var3","var4","var5")
      A2 <- c("var1","var2","var3","var4","var5")
      B <- c(100, 101, 201, 333, 444)
      C <- c(100, 101, 201, 333, 444)
      temp <- data.frame(A1, A2, B, C)
      colnames(temp) <- c("","","1","2")
      return(temp)
    })


    # Table 1

    output$out_best <- DT::renderDataTable({
        DT::datatable(best_sols(), caption = "", rownames = TRUE, options = list(autoWidth = TRUE, scrollX = TRUE, columnDefs = list(list(width = '100px', targets = 1)), paging = FALSE, searching = FALSE), selection='none') %>% formatRound(1:5, 3)
      #}
    })

    # Table 2

    output$out_scorecard <- DT::renderDataTable({
        DT::datatable(score_seg(), caption = "", rownames = F, options = list(autoWidth = TRUE, scrollX = TRUE, columnDefs = list(list(width = '200px', targets = 1)), paging = FALSE, searching = FALSE), selection='single') %>% formatRound(1:5, 3)
    })


    # Table 3
    output$prop_by_cluster_ind <- DT::renderDataTable({
        DT::datatable(profile_result_ind(), class= 'compact stripe', caption = '', rownames = F, options = list(autoWidth = TRUE, scrollX = TRUE, columnDefs = list(list(width = '300px', targets = 1), list(className = 'dt-left', targets="_all")), paging = FALSE, searching = FALSE)) %>% formatStyle(as.character(seq(1:2)))   
    }) 
}

2 个答案:

答案 0 :(得分:2)

我明白了。

如果我们将autoWidth选项更改为FALSE,则标题将正确对齐。

答案 1 :(得分:0)

我有一个具有长行名的表(例如您),并且偏移列名也有类似的问题,但是设置 autoWidth = FALSE 并不能解决问题。我发现这是由 scrollX = TRUE 引起的。我更改了 ScrollX = FALSE ,并使用 overflow-x = TRUE 将数据表包装在div中以重新获得滚动功能:

div(style="overflow-x:auto",renderDataTable({tableName},options=list(scrollX=FALSE))