Search code examples
rshinydt

Extract data from multiple selected data tables rows in ui for use on server side


I have a basic example below to show the functionality. I want to use values from certain columns of selected rows to perform calculations. I am able to get and use those values if I select a SINGLE row. But I haven't figured out how to extract those values in a usable way for multiple rows.

Ultimately I will have to use the values in an SQL statement that grabs data from a database that matches them, but I need a data frame of the values before I can build the SQL statement.

UI

shinyUI(tagList(
  useShinyalert(),
  useShinyjs(),
  
  
  navbarPage(title = "Tree Visualizer",
             tabsetPanel(
               id = "mainTabset",
               tabPanel(
                 title = "Explore Tree",
                 class = "inputs",
                 column(
                   12,
                   
                   
                   
                   selectInput(
                     inputId = "tree_type",
                     label = "Would you like to view a single sample, or cluster multiple samples?",
                     choices = c(
                       Choose = '',
                       Single = 'single',
                       Multiple = 'multiple'
                     ),
                     selectize = FALSE
                   ),
                   
                   conditionalPanel(
                     condition = "input.tree_type == 'single'",
                     DT::dataTableOutput("tbl1"),
                     actionButton(
                       "button1",
                       "SUBMIT",
                       style = "background-color:#221B70;
                       color:#E0EB15;
                       border-color:#E61029;
                       border-style:double;
                       border-width:4px;
                       border-radius:50%;
                       font-size:19px;"
                     ),
                     verbatimTextOutput('x4')
                     ),
                   
                   conditionalPanel(
                     condition = "input.tree_type == 'multiple'",
                     DT::dataTableOutput("tbl2"),
                     actionButton(
                       "button2",
                       "SUBMIT",
                       style = "background-color:#221B70;
                       color:#E0EB15;
                       border-color:#E61029;
                       border-style:double;
                       border-width:4px;
                       border-radius:50%;
                       font-size:19px;"
                     ),
                     verbatimTextOutput('x5')
                     
                     )
                   
                   )
                 
                   )
                     ))
                 ))

SERVER

shinyServer(function(input, output, session) {
  session$onSessionEnded(stopApp)
  
  output$tbl1 <- DT::renderDataTable({
    mtcars
  }, selection = 'single',
  class = "display nowrap compact",
  filter = "top",
  extensions = 'Scroller')
  output$tbl2 <-
    DT::renderDataTable({
      mtcars
    }, selection = 'multiple',
    class = "display nowrap compact",
    filter = "top",
    extensions = 'Scroller')
  
  
  #button1
  observeEvent(input$button1, {
    output$x4 = renderPrint({
      row_count <- input$tbl1_rows_selected
      data <- mtcars[row_count, ]
      id1 <- rownames(data[1,])
      id2 <- data[, 1]
      id3 <- data[, 7]
      
      
      cat('\n\nSelected rows:\n\n')
      cat(id1, id2, id3)
    })
    
    
  })
  
  #button2
  observeEvent(input$button2, {
    output$x5 = renderPrint({
      validate(need(
        length(input$tbl2_rows_selected) > 1,
        "Please choose two or more samples."
      ))
      cat('\n\nSelected rows:\n\n')
      cat(input$tbl2_rows_selected, sep = ', ')
      #create dataframe of selected row properties
      #for example if rows 4, 3, and 6 are selected:
      #car name, mpg, qsec
      #Hornet 4 Drive, 21.4, 19.44
      #Datsun 710, 22.8, 18.61
      #Valiant, 18.1, 20.22
    })
  })
  
  
})

GLOBAL

suppressWarnings({
  suppressPackageStartupMessages({
    library(shiny)
    library(shinyjs)
    library(tidyverse)
    library(shinyalert)
    library(DT)
    
  })
})

Solution

  • That was a lot simpler than I imagined.

    #button2
      observeEvent(input$button2, {
        output$x5 = renderPrint({
          validate(need(
            length(input$tbl2_rows_selected) > 1,
            "Please choose two or more samples."
          ))
          cat('\n\nSelected rows:\n\n')
          row_data <- mtcars[input$tbl2_rows_selected,c(1,7)]
          cat(rownames(row_data[1,0]),row_data[1,1],row_data[1,2])
    
        })
      })