Make datatable #DT rownames not clickable - r

I have a table made with the DT package where my cells are clickable. However, I would like to have the cells in the row names not clickable or in other word, specify a range of columns, something like ‘selected = [2:5]’ where cells are actually active for a click base behavior.
Hope the problem is quiet enough visual. Thanks for your help.

You mean the click for the selection ? The code below works for me but I'm not using the latest version of DT.
output$dtable <- renderDT({
datatable(iris, extensions = "Select", selection = "none",
options = list(
columnDefs = list(
list(className = "not-selectable", targets = 0)
),
select = list(
style = "single",
selector = "td:not(.not-selectable)"
)
)
)
})

Related

Datatables Buttons to show/hide RowGroups

I have a table that uses RowGroups and Buttons/Colvis.
Much like the buttons for Colvis, I'd like to have buttons to show only specific RowGroups. Currently I have a long table with quite a few RowGroups. Often users will want to see only specific column groups and I'd like to prevent them from having to scroll down all the way constantly.
From the examples below, imagine there being a button "Row group" visibility, exactly like the Column visiblity in Colvis.
What I described can be accomplished using the SearchPanes extension. You can select your searchPanes targets on the same columns as your rowGroup targets.
You can also set your searchPanes to be a button.
Example from my code, with use in renderDT:
output$tbl <- renderDT({
datatable(data,
rownames = FALSE,
caption = 'Some caption',
extensions = c('ColReorder', 'RowGroup', "Buttons",
"Select", "SearchPanes"),
options = list(#DOM options
dom = "Bfrtip",
#Rowgroup options
rowGroup = list(dataSrc = c(0,2)),
#Buttons options
stateSave = TRUE,
buttons = c('colvis', 'csv', 'excel', 'searchPanes'),
#ColReorder options
colReorder = TRUE,
#SearchPanes options
columnDefs = list(
list(searchPanes = list(show = FALSE), targets = 1:7)),
selection = 'none',
#Overige options
paging = FALSE)
)}, server = FALSE)
Also credit to Matt Herman's blog post: https://mattherman.info/blog/dt_searchpanes/

R Shiny DT Row Select and Row Edit Collision

I have a Shiny app that I am working on and am using renderDataTable to display a data frame to the user. Right now, the user needs to click on a row of the table to pull up additional information about that row. At the same time, I have the data table set to be 'row' editable. This is really causing some problems. In order to initiate the edit mode for the row, one needs to double click on the row, but clicking multiple times toggles the selected state of the row.
Is there a way to initiate the row editing without having to double click or to disable the row selection status when a double click is present?
EDIT: Here is my invocation of the DT:
output$image_list = DT::renderDataTable({
if(!('data.frame' %in% class(values$images))) {
return(NULL)
}
datatable(values$images,
rowname=FALSE,
options=list(columnDefs = list(list(visible=FALSE, targets=c(0, 1, 3, 6)))),
colnames=c('ID', 'Full File Name', 'Filename', 'Directory', 'Range Scale', 'Heading', 'Status'),
selection = 'single',
editable = list(target='row', disable = list(columns=c(0, 1, 2, 3, 6)))
) %>%
formatStyle('Status', target='row', backgroundColor = styleEqual(c('Incomplete', 'Complete'), c('#FF9999', '#99FF99')))
})
Version Information
Tool | Version
-----|--------
R | 4.0
Shiny| 1.5.0
DT | 0.15
I'm not sure to understand but maybe this can help. With the app below, you can select a row only by clicking on a cell in a non-editable column. So, double-clicking an editable cell doesn't trigger the row selection. Not sure this helps... tell me what.
library(shiny)
library(DT)
dat <- iris[1:6,]
nonEditableColumns <- c(3, 4)
ui <- fluidPage(
br(),
DTOutput("dtable")
)
server <- function(input, output, session) {
output[["dtable"]] <- renderDT({
datatable(
dat,
extensions = "Select",
selection = "none",
editable = list(
target = "row",
disable = list(columns = nonEditableColumns)
),
options = list(
columnDefs = list(
list(className = "selectable", targets = nonEditableColumns),
list(className = "dt-center", targets = "_all")
),
select = list(style = "single",
selector = "td.selectable")
)
)
}, server = FALSE)
}
shinyApp(ui, server)

R large datatable display in Shiny

I am trying to display a relatively large DataTable in a shiny app. The table's dimensions are 7000x30. The performance in Chrome was very very sluggish so I added the 'Scroller' extension to limit the rendering but that did not help. It seems that it is the large number of columns that is causing the issue, not the large number of rows. I also tried to used the colvis extension but the button gives a non scrollable list and with 30 columns, that wouldn't work. Also I tried to have some columns hidden using the visible option, but that didn't work.
Here is the example:
data = as_tibble(matrix(runif(200000), ncol=30))
data %>%
DT::datatable(filter = 'top', extensions = c('Buttons', 'Scroller'),
options = list(scrollY = 650,
scrollX = 500,
deferRender = TRUE,
scroller = TRUE,
# paging = TRUE,
# pageLength = 25,
buttons = list('excel',
list(extend = 'colvis', targets = 0, visible = FALSE)),
dom = 'lBfrtip',
fixedColumns = TRUE),
rownames = FALSE)
Weirdly, the Rstudio viewer shows the datatable and is fine. It's only when I Run the document as a shiny document and open it in Chrome that it becomes very sluggish. My questions are:
Why is this happening
How do I only show a limited number of columns by default and have the option to show the others
is there a better button for colvis? if the list of columns exceeds the page length, I can't access those hidden columns to toggle them on or off.
What do you mean by sluggish?
I ran it and everything looks okay to me in terms of speed.
library(shiny)
library(shinydashboard)
library(DT)
####/UI/####
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(
DT::dataTableOutput("test")
)
ui <- dashboardPage(header, sidebar, body)
####/SERVER/####
server <- function(input, output, session) {
data <- as_tibble(matrix(runif(200000), ncol=30))
output$test <- DT::renderDataTable({
DT::datatable(
data,
filter = 'top', extensions = c('Buttons', 'Scroller'),
options = list(scrollY = 650,
scrollX = 500,
deferRender = TRUE,
scroller = TRUE,
# paging = TRUE,
# pageLength = 25,
buttons = list('excel',
list(extend = 'colvis', targets = 0, visible = FALSE)),
dom = 'lBfrtip',
fixedColumns = TRUE),
rownames = FALSE)
})
}
shinyApp(ui, server)
You may want to remove 'scroller' from Options = () as with that many rows, you may want to break it up into pages. Also, you can try to make the table server processing by putting Server = TRUE in there, that will make it process every page versus the whole dataset at once.

Copy a Shiny DT row to users clipboard

Is there a way to have the selected row(s) in a shiny datatable (DT) be available for the user to copy (Ctrl+C) to their clipboard. Ideally it would also supply the data table's column names or headers.
UPDATE
global.R
library(rclipboard)
library(shiny)
ui.R:
...
rclipboardSetup(),
...
uiOutput("copy"),
server.R:
output$copy = renderUI({
s = input$orders_rows_selected
rclipButton("copybtm","Copy",data()[s,],icon("clipboard"))
})
Here is how to get a button to copy the selected rows. And there are the column headers too.
datatable(
iris,
rownames = FALSE,
extensions = c("Buttons", "Select"),
options =
list(
select = TRUE,
dom = "Bfrtip",
buttons = list(
list(
extend = "copy",
text = 'Copy',
exportOptions = list(modifier = list(selected = TRUE))
)
)
)
)

Remove header row in datatable

I'm trying to remove the header row in a DT datatable, does anyone know if there's an option for doing this?
Minimal example:
#SERVER.R
output$myTable <- renderDataTable({
datatable(dataset, rownames = FALSE, selection = 'none', options = list(dom = 't'))
})
#UI.R
dataTableOutput('myTable')
Just add colnames = NULL to your datatable()
datatable(mtcars, rownames = FALSE,colnames=NULL, selection = 'none', options = list(dom = 't'))
See ?datatable
Sebastian's answer from Oct 28 '15 did not work for me inside renderDataTable(datatable(...)). Using colnames = NULL inside datatable(...) removed all data from the table. I had to use colnames = "" to remove the table headers.
Adding colnames = NULL no longer works and it renders an empty table. You can use headerCallBack option to add a css that hides the header row to achieve this.
headerCallback <- c(
"function(thead, data, start, end, display){",
" $('th', thead).css('display', 'none');",
"}"
)
datatable(mtcars,
rownames = FALSE,
colnames=NULL,
selection = 'none',
options = list(
dom = 't',
headerCallback = JS(headerCallback)
)
)

Resources