Add Cell Borders in an R Datatable

自闭症网瘾萝莉.ら 提交于 2019-12-10 12:06:42

问题


Fairly new to R - doing OK with big picture stuff, and struggling on cleaning up the edges when I want to present something to other people.

Banging my head against the wall with something that's probably pretty simple - I simply want to add cell borders - to all cells - in a datatable in a shiny app. Here's a relevant chunk of code:

library(ggplot2)
library(shiny)
library(data.table)
library(DT)
library(plotly)

setwd("C:/Users/Will/Desktop/FinalPages")

lister <- read.table("PlayerList.csv", header=TRUE, quote ="", sep=",",fill = TRUE)
totals <- read.table("TotShooting.csv", header=TRUE, quote ="", sep=",",fill = TRUE)

items <- as.character(lister[[1]])

ui <- fluidPage(

  sidebarLayout(

    sidebarPanel(selectizeInput("players", "Player:", choices = items, multiple = FALSE),
    width=2

              ),

    mainPanel(h5("Total Shooting", align = "center"),
              div(dataTableOutput("tot"), style = "font-size:80%", class = 'table-condensed cell-border row-border'),
              position="center",
              width = 10)
  )
)

server <- function(input, output) {

  output$tot <- DT::renderDataTable({

    validate(
      need(input$players, ' ')
    )

    filterone <- subset(totals, Name == input$players)

    filterone <- filterone[,-1:-2]

    DT::datatable(filterone,
                  rownames = FALSE,

                  options=list(iDisplayLength=7,                    
                               bPaginate=FALSE,                  
                               bLengthChange=FALSE,                       
                               bFilter=FALSE,                                    
                               bInfo=FALSE,
                               rowid = FALSE,
                               autoWidth = FALSE,
                               ordering = FALSE,
                               scrollX = TRUE,
                               borders = TRUE,
                               columnDefs = list(list(className = 'dt-center', targets ="_all"))
                  ))
  }
  )

I've been trying to track it down via google, but haven't been able to hit on a solution I can get to work. It's probably something very simple with tags, or a correct class name (I hope so, at least), but I'm lost here. Appreciate any help I can get.


回答1:


The function that you are looking for is : formatStyle("your DT table", "vector of column index", border = '1px solid #ddd').

You can find a reproducible example here :

library(shiny)
library(DT)

shinyApp(
  ui = fluidPage(
   DT::dataTableOutput("test")
  ),

 server = function(input, output, session) {
   output$test <- DT::renderDataTable({
     datatable(mtcars) %>% 
     formatStyle(c(1:dim(mtcars)[2]), border = '1px solid #ddd')
   })
})

There must be more elegant ways but it works !



来源:https://stackoverflow.com/questions/50751568/add-cell-borders-in-an-r-datatable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!