Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny Datatable column split with line

Tags:

r

datatable

shiny

I generate a Datatable in my shiny server like:

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

And have a Ui like:

 tableOutput("x")

Now I want that every second column a split is marked as line. For example:

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

I hope someone can help me. Thanks

like image 231
Zorro Avatar asked May 17 '26 17:05

Zorro


1 Answers

I've used a subset of mtcars dataset to generate this output. Below is a self-sufficient reproducible example for the same. One has to use CSS Styling to achieve this effect. Additional Information for advanced Styling is available 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)

Attached is a snap-shot of the formatted table from the UI.

A Snapshot from the UI generated from the code

like image 143
Ravi Krishna Avatar answered May 21 '26 00:05

Ravi Krishna



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!