firmo23
firmo23

Reputation: 8454

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

Upvotes: 0

Views: 73

Answers (1)

YBS
YBS

Reputation: 21349

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

Upvotes: 1

Related Questions