-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Select rows using checkboxes #93
Comments
This might be relevant |
Hi, Many thanks. This is certainly relevant. My question now is how to show the table with a certain number of rows selected by default. This was very easy to do with checkboxes but I am not sure how to do it now. Thanks! Virgilio |
I added also a note here: #89 |
Hi, You can use something like $("#mytable tbody tr")[0].click() in Javascript to click on an initial set of rows. The problem is that I do not know how to run the JS code on startup only... V |
You should be able to use the initComplete option in DataTables to call the javascript function, but it doesn't look like it works for rows beyond the first page:
|
Hi, Many thanks for the hint. This is really useful and I have been able to update my code to the latest version! :) Regarding your comment about it only working in the first page, you may look at how select rows using server-side selection: https://yihui.shinyapps.io/DT-rows/ I am sure that you can find a way of defining an index that will span the whole table and not the current page. Many thanks! Virgilio |
I tried to merge the example from @ajholguin with https://yihui.shinyapps.io/DT-rows/ with no luck. List of issues:
Any ideas?
|
So there are actually two issues blended into this discussion. Let's keep the discussion of pre-selected rows in #89 only. For checkboxes, I just prepared an example in the shiny-discuss mailing list: https://groups.google.com/forum/#!topic/shiny-discuss/FeqU0AoTpz0 library(shiny)
library(DT)
shinyApp(
ui = fluidPage(DT::dataTableOutput('x1'), verbatimTextOutput('x2')),
server = function(input, output) {
# create a character vector of shiny inputs
shinyInput = function(FUN, len, id, ...) {
inputs = character(len)
for (i in seq_len(len)) {
inputs[i] = as.character(FUN(paste0(id, i), label = NULL, ...))
}
inputs
}
# obtain the values of inputs
shinyValue = function(id, len) {
unlist(lapply(seq_len(len), function(i) {
value = input[[paste0(id, i)]]
if (is.null(value)) NA else value
}))
}
# a sample data frame
res = data.frame(
v1 = shinyInput(numericInput, 100, 'v1_', value = 0),
v2 = shinyInput(checkboxInput, 100, 'v2_', value = TRUE),
v3 = rnorm(100),
v4 = sample(LETTERS, 100, TRUE),
stringsAsFactors = FALSE
)
# render the table containing shiny inputs
output$x1 = DT::renderDataTable(
res, server = FALSE, escape = FALSE, selection = 'none', options = list(
preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
)
)
# print the values of inputs
output$x2 = renderPrint({
data.frame(v1 = shinyValue('v1_', 100), v2 = shinyValue('v2_', 100))
})
}
) This is a more general solution than checkboxes -- you can use arbitrary shiny inputs in the table, e.g. numericInput, textInput, checkboxInput, and so on. |
I am trying to implement deletion of selected (using checkboxes) rows but strange things are happening. Any hint on how to implement deletion of selrcted rows highly appreciated. Please run the code below to see the problem (select just a few rows using checkboxes). When the app shows in browser select first 7 rows. Click "Delete Selected" button. Then you are left with three rows and all three checkboxes are not checked. Then if you press "Delete Selected" button again all three rows (although unchecked) are deleted. I tried to use updateCheckboxInput() but without success.
|
Selecting rows and pre-selecting rows are both possible with the current development version now, so I'm closing this issue. Please check out the documentation http://rstudio.github.io/DT/shiny.html and example https://yihui.shinyapps.io/DT-selection You are welcome to open a new issue if you still think checkboxes are better than the current way of row selection. |
Hi, Thanks for your answer. I have a use case when I need both, check boxes and Best regards, 2015-07-19 0:23 GMT-04:00 Yihui Xie notifications@github.com:
|
I'm trying to reuse @yihui's example except I need to be able to change the underlying datatable itself. I think the reactive link gets broken when I reuse the I also tried running an explicit What can I do? App example below -
|
See this SO answer: http://stackoverflow.com/questions/40020600/r-shiny-selectedinput-inside-renderdatatable-cells/40027726#40027726 Per @yihui here: https://groups.google.com/forum/#!msg/shiny-discuss/ZUMBGGl1sss/7sdRQecLBAAJ When you rerender the table you need to add an extra function to unbind. It will generate a javascript error because it will try to unbind on load before the table exists, but it's a benign error me thinks. |
hello, i'm using the 2 functions of @yihui for my app, but i'm having problem with the app example below: library(shiny)
library(DT)
# create a character vector of shiny inputs
shinyInput <- function(FUN, len, id, ...) {
inputs <- character(len)
for (i in seq_len(len))
inputs[i] <- as.character(FUN(paste0(id, i), label = NULL, ...))
inputs
}
# obtain the values of inputs
shinyValue <- function(input, id, len)
unlist(lapply(seq_len(len), function(i) {
value <- input[[paste0(id, i)]]
if (is.null(value)) NA else value
}))
shinyApp(
ui = fluidPage(
numericInput(inputId = "n", value = 20, label = "No. of rows"),
actionButton(inputId = "unselectAll", label = "Unselect all", icon = icon("minus")),
DT::dataTableOutput("x1"),
verbatimTextOutput("x2")
),
server = function(input, output, session) {
# a sample data frame
res = reactive({
data.frame(
v2 = shinyInput(checkboxInput, input$n, "v2_", value = TRUE),
v3 = rnorm(input$n),
v4 = sample(LETTERS, input$n, TRUE),
stringsAsFactors = FALSE
)
})
# render the table containing shiny inputs
output$x1 = DT::renderDataTable(res(),
selection = "none", rownames = FALSE, escape = FALSE,
extensions = c("Buttons", "FixedColumns"),
options = list(
preDrawCallback = JS("function() {Shiny.unbindAll(this.api().table().node());}"),
drawCallback = JS("function() {Shiny.bindAll(this.api().table().node());}"),
scrollX = TRUE, scrollY = "500px",
dom = "Bfrtip",
fixedColumns = list(leftColumns = 1)
)
)
# button select all
observeEvent(input$unselectAll, {
for (i in input$x1_rows_all)
updateCheckboxInput(session, inputId = paste0("v2_", i), value = FALSE)
})
# print the values of inputs
output$x2 = renderPrint({
data.frame(v2 = shinyValue(input, "v2_", input$n))
})
}
) |
I encounter a strange behavior, when I try to combine these checkBoxes in DT table with editable cells. I have built upon yihui's code and try to make the Letters column editable. However, when I check some boxes in the first column, make edits in the second column (the letters) and then click somewhere, the boxes are all unchecked again. I suppose it's due to the proxy refreshing, but how can I overcome this? ` shinyApp(
) EDIT: I found my mistake. Changing the code to: solved the issue. I leave the code snippet, in case someone finds it helpful. |
You can use the checkboxes of the Select extension. But there's something strange: one sees the row names. One didn't see them before. library(DT)
dat <- iris
datatable(
dat, #rownames = FALSE,
extensions = "Select", selection = "none",
options = list(
columnDefs = list(
list(targets = 0, orderable = FALSE, className = "select-checkbox")
),
select = list(
style = "multi", selector = "td:first-child"
)
)
) |
Hi,
I have been using DT to show a table where columns could be selected using checkboxes (in the row names) and the column ids saved into an .RData file. However, after updating shiny and DT, this feature seems not to be working. Also, the page where I took the example from (https://yihui.shinyapps.io/DT-checkbox/) seems to have disappeared, so I presume that this has something to do with the latest release of shiny, DT and/or DataTable. Is there an easy fix to this?
I am using the latest version of shiny and DT from github.
Best,
Virgilio
The text was updated successfully, but these errors were encountered: