firmo23
firmo23

Reputation: 8404

Subset a data table based on cell selection of another datatable

I have a shiny app with 3 datatables. The first one contains only the 5th row with Species the 2nd table contains some random Species and the third table should get the selected cells from the first 2 tables and combines them into a new table. While the first seems to works fine the 2nd table selection seems to be wrong and that happens because it subsets by cell position and not by cell value.

library(shiny)
library(DT)
data("mtcars")

ui <- shinyUI(
  fluidRow(
    DT::dataTableOutput("myDatatable"),
    DT::dataTableOutput("myDatatable2"),
    DT::dataTableOutput("myDatatable3")

  )

)

server <- shinyServer(function(input, output, session) {
  output$myDatatable <- DT::renderDataTable(matrix(iris[,5]), 
                                            selection=list( target="cell"),
                                            server = FALSE,
                                            rownames=FALSE)
  output$myDatatable2 <- DT::renderDataTable(matrix(iris[c(25,78,67,45,90,66,78,9,8),5]), 
                                            selection=list(mode="single", target="cell"),
                                            server = FALSE,
                                            rownames=FALSE)
  output$myDatatable3 <- DT::renderDataTable(iris[c(input$myDatatable_cells_selected,input$myDatatable2_cells_selected),], 
                                             server = FALSE,
                                             rownames=FALSE)
})

shinyApp(ui, server)

Upvotes: 0

Views: 359

Answers (1)

Kevin
Kevin

Reputation: 2044

See code below. You were trying to subset the data versus creating a new dataframe(which from above sounds like what you want) + you want to use [tablename]_cell_clicked which has a row, column, value list versus [tablename]_cells_selected.

library(shiny)
library(DT)
data("mtcars")

ui <- shinyUI(
  fluidRow(
    DT::dataTableOutput("myDatatable"),
    DT::dataTableOutput("myDatatable2"),
    DT::dataTableOutput("myDatatable3")

  )

)

server <- shinyServer(function(input, output, session) {


  dat1 <- reactive({
    matrix(iris[,5])
  })

  dat2 <- reactive({
    matrix(iris[c(25,78,67,45,90,66,78,9,8),5])
  })

  dat3 <- reactive({
    temp <- data.frame(results = c(input$myDatatable_cell_clicked$value, input$myDatatable2_cell_clicked$value))
  })




  output$myDatatable <- DT::renderDataTable(dat1(), 
                                            selection=list( target="cell"),
                                            server = FALSE,
                                            rownames=FALSE)
  output$myDatatable2 <- DT::renderDataTable(dat2(), 
                                             selection=list(mode="single", target="cell"),
                                             server = FALSE,
                                             rownames=FALSE)
  output$myDatatable3 <- DT::renderDataTable(dat3(), 
                                             server = FALSE,
                                             rownames=FALSE)
})

shinyApp(ui, server)

**Updated based on OP's clarification

library(shiny)
library(DT)
data("mtcars")

ui <- shinyUI(
  fluidRow(
    DT::dataTableOutput("myDatatable"),
    DT::dataTableOutput("myDatatable2"),
    DT::dataTableOutput("myDatatable3")

  )

)

server <- shinyServer(function(input, output, session) {


  dat1 <- reactive({
    matrix(iris[,5])
  })

  dat2 <- reactive({
    matrix(iris[c(25,78,67,45,90,66,78,9,8),5])
  })

  dat3 <- reactive({

    dat1row <- input$myDatatable_cells_selected
    dat2row <- c(25,78,67,45,90,66,78,9,8)[c(input$myDatatable2_cell_clicked$row)]


    temp <- iris[c(dat1row, dat2row),]
  })




  output$myDatatable <- DT::renderDataTable(dat1(), 
                                            selection=list( target="cell"),
                                            server = FALSE,
                                            rownames=FALSE)
  output$myDatatable2 <- DT::renderDataTable(dat2(), 
                                             selection=list(mode="single", target="cell"),
                                             server = FALSE,
                                             rownames=FALSE)
  output$myDatatable3 <- DT::renderDataTable(dat3(), 
                                             server = FALSE,
                                             rownames=FALSE)
})

shinyApp(ui, server)

Upvotes: 2

Related Questions