简体   繁体   中英

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

I have the shiny app below in which when I click on a row of the 1st table I should get the correspondent value of species column. Then with this value I should subset the second dataframe df based on its species column.

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

Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM