R Shiny-使用DataTable的列名称已移位 [英] R Shiny - Shifted column names with DataTable

查看:72
本文介绍了R Shiny-使用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 ?

可复制的示例:

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)

我找到了这些主题:

如何解决数据表中闪亮的未对准问题?

列标题未正确对齐

但是它没有提供解决方案,我需要autowidth = T,并且设置列大小不会改变未对齐问题.

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

谢谢

推荐答案

您需要将表放置在一个容器中,该容器会将内容(包括DT表标题)居中.可以在保留当前边距的同时,通过将您的段落标签替换为 fluidRow(column(align ="center",... .... .),我在下面调整了您的代码:

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)

这篇关于R Shiny-使用DataTable的列名称已移位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆