简体   繁体   中英

Shiny DataTable column width by name

I have a DataTable in a Shiny app and the user is allowed to select the columns. I want to control the width of the column but that column's index changes depending on user selections so I want to control the width based on the column name.

In the reproducible example below (which is just an example of what I want to do), I would like to control the width of column "c". At first I know the index so this is fine, but when the user adds a new column ("b") the column width formatting gets applied to whatever column is second (index = 1 ).

Note that the documentation for DataTables suggests that using target=c("c") would be possible but this link seems to say that the functionality does not work.

    library(shiny)
    ui <- shinyUI(

      tagList(

        navbarPage(title = HTML('<span class="navtitle">Test</span>'),
                   id= "myid", 
                   inverse=TRUE, 
                   collapsible = TRUE,
                   tabPanel("Home", 
                            selectInput('myselect', label="", choices=c("a", "b", "c"),
                                        selected = c("a", "c"), multiple=TRUE),
                            dataTableOutput(outputId="dataTable")
                   )
        ) 
      )  
    )

    server <- function(input, output, session){ 

      output$dataTable <- renderDataTable({
        dat <- data.frame(a=1:10, b=1:10, c=1:10)
        dat[, names(dat)%in%input$myselect]


      }, 

      options = list(pageLength = 100, dom='frtp', 
                     scrollX = TRUE, 
                     columnDefs = list(list(width = '30px', targets = c(1))))
      )

    }

shinyApp(ui=ui, server=server)

Thanks to @Yihui for the answer at this post

You can use a function in the options, very useful

library(shiny)
ui <- shinyUI(

  tagList(

    navbarPage(title = HTML('<span class="navtitle">Test</span>'),
               id= "myid", 
               inverse=TRUE, 
               collapsible = TRUE,
               tabPanel("Home", 
                        selectInput('myselect', label="", choices=c("a", "b", "c"),
                                    selected = c("a", "c"), multiple=TRUE),
                        dataTableOutput(outputId="dataTable")
               )
    ) 
  )  
)

server <- function(input, output, session){ 

  output$dataTable <- renderDataTable({
    dat <- data.frame(a=1:10, b=1:10, c=1:10)
    dat[, names(dat)%in%input$myselect]


  }, 

  options = function(){
    indx <- c(which("b"%in%input$myselect))
    if(length(indx)==0) index <- ''

    list(
      pageLength = 100, 
      dom='frtp',
      scollX = TRUE,
      columnDefs = list(list(width = '30px', targets = indx))
    )
  }
  )

}

shinyApp(ui=ui, server=server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM