Problem when select rows in Datatable inside of modalDialog.

298 views
Skip to first unread message

pablodp

unread,
Jan 3, 2017, 4:38:28 PM1/3/17
to Shiny - Web Framework for R
Hi all, I am working in a project where I need to add a button, when you click it, a modal is open and you see a table where you can select rows.
I am working wih  modalDialog and this don't keep the selected rows when I close modal and open again.
I tested working with shinyBS package and this worked for me. But I would like to implement with modalDialog.

This is my code  using modalDialog ( I created 2 functions for server and UI because I need to reuse this code in differents modules):

GroupFilterUI <- function(ns) {
  fixedRow(
    div(
    actionButton(ns("selectgroup"), "Select Group")
    ),
    div(textOutput(ns("groupsSelected")))
  )
}

GroupFilterServer <- function(output,input, session, data){
  
  # Observe for selecgroup actionbutton.
  observeEvent(input$selectgroup, {
    showModal(modalDialog(DT::dataTableOutput(session$ns("groupslist")),
                          title="Select Group",
                          footer = modalButton("Close")))
  })
  
  # Table to show all groups availables in course.
  output$groupslist <- DT::renderDataTable(data,
                                          colnames = c("Group Id" = "groupId",
                                                       "Group Name" = "name"),
                                          caption = "Groups",
                                          selection = 'multiple',
                                          rownames = FALSE,
                                          escape = FALSE) 
  
  output$groupsSelected <- renderText({
    
    if(is.null(input$groupslist_rows_selected)) {
      ret <-"Showing all groups in course."
    } else {
      g <- data[input$groupslist_rows_selected,]
      ret <- paste(g$name)
    }

    ret
  })
}



And this is my code with shinyBS (working perfectly):
GroupFilterUIBS <- function(ns) {
  fixedRow(
    div(
      actionButton(ns("selectgroup"), "Select Groups"),
      bsModal(ns("bs"),"Select Groups",ns("selectgroup"),DT::dataTableOutput(ns("groupslist")))
    ),
    div(textOutput(ns("groupsSelected")))
  )
}

GroupFilterServerBS <- function(output,input, session, data){
  
  # Table to show all groups availables in course.
  output$groupslist <- DT::renderDataTable(data,
                                           colnames = c("Group Id" = "groupId",
                                                        "Group Name" = "name"),
                                           caption = "Groups",
                                           selection = 'multiple',
                                           rownames = FALSE,
                                           escape = FALSE)  
  
  output$groupsSelected <- renderText({
    
    if(is.null(input$groupslist_rows_selected)) {
      ret <- "Showing all groups in course."
    } else {
      g <- data[input$groupslist_rows_selected,]
      ret <- paste(g$name)
    }
    
    ret
  })
}


Someone has some idea about this problem? I didn't find an example as this. 

Maybe the problem is to renderize the table inside of observerEvent() ?

Thanks in advance.

Reply all
Reply to author
Forward
0 new messages