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)