Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shifted column names with DataTable

Tags:

r

dt

shiny

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.

like image 761
Chika Avatar asked Oct 24 '25 19:10

Chika


1 Answers

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)
like image 195
da11an Avatar answered Oct 26 '25 10:10

da11an



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!