2017-06-20 24 views
1

我有一个数据表,其中包含一些非常宽的列,我想添加一个滚动条使其更易于表现。到目前为止,我已经找到了使用整个表的滚动条的示例 - 但理想情况下,如果可能的话,我希望在表中为每个列提供一个滚动条。下面有一个例子。在这段代码我想要一个滚动条两个“This_is_a_very_long_name_1”,“This_is_a_very_long_name_2”等R Shiny/Shinydashboard:在表格中隐藏字符串的最后部分

library("shinydashboard") 
library("shiny") 

body <- dashboardBody(
    fluidPage(
    column(width = 4, 
      box(
      title = "Box title", width = NULL, status = "primary", 
      div(style = 'overflow-x: scroll', tableOutput('table')) 
     ) 
    ) 
) 
) 

ui <- dashboardPage(
    dashboardHeader(title = "Column layout"), 
    dashboardSidebar(), 
    body 
) 

server <- function(input, output) { 
    test.table <- data.frame(lapply(1:8, function(x) {1:10})) 
    names(test.table) <- paste0('This_is_a_very_long_name_', 1:8) 

    output$table <- renderTable({ 
    test.table 
    }) 

} 

# Preview the UI in the console 
shinyApp(ui = ui, server = server) 

我想过拆分表到8桌,使得滚动表中的每个人,然后把他们未来彼此之间,但他们之间增加了空间,它看起来不太好。我认为最好将它保持为一张桌子(但建议非常受欢迎!)。

有没有人这是可能的 - 以及如何解决它?

在此先感谢!

+0

为什么不在两行显示列名?我不认为滚动是列名的好主意 –

+0

感谢您的回答! 上面的代码只是为了说明我的问题(但事后看来它可能无法很好地捕捉到它)。在我的实际问题是不是头宽太宽,但在下面的数据(在这个例子中,它是1,...,10显示 - 这些是非常长的线/字符串)。这个建议很好,但是如果我把每一行分成更多行,那么我可能最终只显示5个观察值)。 我的“愿望”是以某种方式隐藏长字符串的最后部分,然后仅在滚动该列时才显示结尾。 我希望它是有道理的。 – Dorthe

+0

好吧等一下我知道你可以做什么,我会更新我的答案 - >仅仅为了将来你应该清楚地写出问题,它不是关于列标题,而是关于行文本 –

回答

0

我不会推荐滚动列标题,我认为它不会很清楚地阅读它。这里是你可以用它来获得头2行,因此列不是太宽代码:

library("shinydashboard") 
library("shiny") 
library(DT) 

test.table <- data.frame(lapply(1:8, function(x) {1:10})) 
names(test.table) <- paste0('This_is_a_very_long_name_', 1:8) 

body <- dashboardBody(
    fluidPage(
    column(width = 8, 
      box(
      title = "Box title", width = NULL, status = "primary", 
      div(style = 'overflow-x: scroll', dataTableOutput('table')) 
      ) 
    ) 
) 
) 

ui <- dashboardPage(
    dashboardHeader(title = "Column layout"), 
    dashboardSidebar(), 
    body 
) 

server <- function(input, output) { 

    output$table <- renderDataTable({ 
    names(test.table) <- gsub("_"," ",names(test.table)) 
    datatable(test.table, options = list(columnDefs = list(list(width = '100px', targets = c(1:8))))) 
    }) 

} 

# Preview the UI in the console 
shinyApp(ui = ui, server = server) 

[更新] - >列文本渲染

这里是一个解决方案这对你有用。没有滚动条,但你行文字显示只有前三个字符(显示字符数是可以改变的)和...,用鼠标移到该行你得到弹出了全局变量名称在此行中:

library("shinydashboard") 
library("shiny") 
library(DT) 

x <- c("aaaaaaaaaaaaaa", "bbbbbbbbbbbb", "ccccccccccc") 
y <- c("aaaaaaaaaaaaaa", "bbbbbbbbbbbb", "ccccccccccc") 
z <- c(1:3) 
data <- data.frame(x,y,z) 

body <- dashboardBody(
    fluidPage(
    column(width = 4, 
      box(
      title = "Box title", width = NULL, status = "primary", 
      div(style = 'overflow-x: scroll', dataTableOutput('table')) 
      ) 
    ) 
) 
) 

ui <- dashboardPage(
    dashboardHeader(title = "Column layout"), 
    dashboardSidebar(), 
    body 
) 

server <- function(input, output) { 

    output$table <- renderDataTable({ 
    datatable(data, options = list(columnDefs = list(list(
              targets = c(1:3), 
              render = JS(
               "function(data, type, row, meta) {", 
               "return type === 'display' && data.length > 3 ?", 
               "'<span title=\"' + data + '\">' + data.substr(0, 3) + '...</span>' : data;", 
               "}")),list(width = '100px', targets = c(1:3))))) 
    }) 

} 

# Preview the UI in the console 
shinyApp(ui = ui, server = server) 
+0

非常感谢!这是完美的! 祝您有美好的一天! – Dorthe