根据数据表的特定行选择的特定列子集数据框

Subset a dataframe based on certain column of certain row selection of a datatable

我有下面的 shiny 应用程序,当我点击第一个 table 的一行时,我应该得到 species 列的对应值。然后使用这个值,我应该根据其 species 列对第二个数据帧 df 进行子集化。

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(DT::dataTableOutput('tableId'),
                 dataTableOutput("celltext")),
  server = function(input, output) {
    output$tableId = DT::renderDataTable(
      iris[,c(1,5)],  selection = list(target = 'row',mode="single")
    )
     species<-c("setosa","setosa","virginica","virginica")
     flower<-c("a","b","c","d")
     score<-c(7,5,6,9)
     df<-data.frame(species,flower,score)
    output$celltext <- renderDataTable({
      cell <- input$tableId_rows_selected
      df<-df[df$species == iris[row]
    })
  }
)

试试这个

shinyApp(
  ui = fluidPage(DT::dataTableOutput('tableId'),
                 DTOutput("celltext")),
  server = function(input, output) {
    output$tableId = DT::renderDataTable(
      iris[,c(1,5)],  selection = list(target = 'row',mode="single")
    )
    species<-c("setosa","setosa","virginica","virginica")
    flower<-c("a","b","c","d")
    score<-c(7,5,6,9)
    df<-data.frame(species,flower,score)
    
    output$celltext <- renderDT({
      cell <- input$tableId_rows_selected
      dat<-df[df$species %in% iris[cell,5],]
      dat
    })
  }
)