R Shiny 从 ui 中的多个选定数据表行中提取数据以供服务器端使用

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

我在下面有一个基本示例来展示功能。我想使用 selected 行的某些列中的值来执行计算。如果我 select 一行,我就能够获取并使用这些值。但我还没有想出如何以一种可用的方式为多行提取这些值。

最终我将不得不在 SQL 语句中使用这些值,该语句从匹配它们的数据库中获取数据,但在构建 SQL 之前我需要一个值的数据框声明。

想法?

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')

                     )

                   )

                   )
                     ))
                 ))

服务器

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
    })
  })


})

全球

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

  })
})

这比我想象的简单多了。

#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])

    })
  })