Chika
Chika

Reputation: 1497

Shifted column names with DataTable

I have a pretty complicated shiny code with several panels and several tables inside these panels. When the app is launched, column names are correctly aligned with the column values. But once I change the page number under my table on the app, the column names get shifted on the left side where as the values remain in the centre. How can I force the app to leave the column names aligned with the column values?

A reproducible example :

library(shiny)
library(dplyr)
library(DT)
library(dplyr)

ui <- fluidPage(
  
  titlePanel("Test Example"), 
  
  fluidRow(
    column(3, align="left",
           
           # Input: Quantile ----
           selectInput(inputId = "Variable",
                       label = "Variable :",
                       choices = c(80, 85, 90, 95),
                       selected = 90)),
    
    column(9,
           tabsetPanel(
             tabPanel("Table d'évènements", verticalLayout(p(dataTableOutput("cars.table")),
                                                           p(dataTableOutput("cars.table2"))))))
  )
)

server <- function(input, output) {
  output$cars.table <- DT::renderDataTable({
    df <- summarise(group_by(cars, speed), n=mean(dist))
    df
  }, options=list(autoWidth = TRUE, scrollX=T, pageLength=5), rownames= FALSE)
  
  output$cars.table2 <- DT::renderDataTable({
    df1 <- summarise(group_by(cars, speed), n=max(dist))
    df1
  }, options = list(autoWidth = TRUE,scrollX=T,pageLength=10),rownames= FALSE)
}

shinyApp(ui = ui, server = server)

I found these topics :

How to solve shiny mis-alignment issue in datatable?

and

Column headers are not correctly aligned

But it didn't provide solutions, I need autowidth=T and setting the column sizes does not change anything to the mis-alignment problem.

Upvotes: 4

Views: 1718

Answers (1)

da11an
da11an

Reputation: 731

You need to place your tables in a container that will center the contents, including the DT table header. This can be done while preserving your current margins by replacing your paragraph tag with fluidRow(column(align = "center", .... I've adjusted your code below:

library(shiny)
library(dplyr)
library(DT)
library(dplyr)

ui <- fluidPage(
  
  titlePanel("Test Example"), 
  
  fluidRow(
    column(3, align="left",
           
           # Input: Quantile ----
           selectInput(inputId = "Variable",
                       label = "Variable :",
                       choices = c(80, 85, 90, 95),
                       selected = 90)),
    
    column(9,
           tabsetPanel(
             tabPanel("Table dvnements", verticalLayout(
               fluidRow(column(align = "center", width = 12, dataTableOutput("cars.table"))),
               fluidRow(column(align = "center", width = 12, dataTableOutput("cars.table2")))))))
  )
)

server <- function(input, output) {
  output$cars.table <- DT::renderDataTable({
    df <- summarise(group_by(cars, speed), n=mean(dist))
    df
  }, options=list(autoWidth = TRUE, scrollX=T, pageLength=5), rownames= FALSE)
  
  output$cars.table2 <- DT::renderDataTable({
    df1 <- summarise(group_by(cars, speed), n=max(dist))
    df1
  }, options = list(autoWidth = TRUE,scrollX=T,pageLength=10),rownames= FALSE)
}

shinyApp(ui = ui, server = server)

Upvotes: 7

Related Questions