【问题标题】:R shiny Datatable column split with lineR闪亮的Datatable列用线分割
【发布时间】:2018-09-22 10:19:02
【问题描述】:

我在闪亮的服务器中生成一个数据表,例如:

x=renderTable(rownames = FALSE,{...}

并且有一个类似的 UI:

 tableOutput("x")

现在我希望每隔一列将拆分标记为行。 例如:

a   b | c   d | e  ...
1   2 | 2   3 | 4  ...
4   3 | 1   2 | 3  ...
3   1 | 5   5 | 5  ...

我希望有人可以帮助我。谢谢

【问题讨论】:

    标签: r datatable shiny


    【解决方案1】:

    我使用了mtcars 数据集的子集来生成此输出。下面是一个自给自足的可重现示例。必须使用 CSS 样式来实现此效果。有关高级样式的其他信息,请访问here

    library(DT)
    library(shiny)
    library(datasets)
    
    
    ui <- fluidPage(
    
      dataTableOutput("table")
    
    )
    
    server <- function(input, output, session) {
    
      mtcars <- mtcars %>% select(1:8)
      row.names(mtcars) <- NULL
    
      output$table <- renderDataTable({
    
        # Initiate Empty Vector for Alternative border formating
        alt_vector <- vector(mode = "numeric")
    
        # Iterate over the no. of columns in the table to generate the vector
        for (i in 1:ncol(mtcars)) { 
          if(i %% 2 == 0) 
            alt_vector <- c(alt_vector,i)
        }
    
          df <- datatable(mtcars,rownames = FALSE, options = list(pageLength = 25)) %>%
                # First Column Border Left
                formatStyle(c(1),`border-left` = '1px solid black') %>% 
                # Rest Alternative Bordering
                formatStyle(alt_vector,`border-right` = '1px solid black')
    
      })
    
    }
    
    shinyApp(ui, server)
    

    附件是来自 UI 的格式化表格的快照。

    【讨论】:

    • 您好,感谢您的帮助,它有效。但我只想要一个包含所有数据的页面。我该如何调整?
    • pageLength 属性可用于实现相同的目的。使用 options = list(pageLength = nrow(mtcars)) 作为创建数据表的参数。
    • 那么,@Zorro 您是否希望输出看起来与我在答案中发布的图像相同?每页 10 行?
    • 在 25 行的答案中包含 pageLength 参数,但可以根据需要进行更改。希望这会有所帮助。
    • 不错的答案,有没有办法将分隔符也扩展到标题中?
    猜你喜欢
    • 2014-10-02
    • 2021-09-02
    • 2014-08-27
    • 2016-02-09
    • 2020-02-04
    • 2021-04-02
    • 2018-06-08
    • 1970-01-01
    • 2016-07-02
    相关资源
    最近更新 更多