Phlybo
Phlybo

Reputation: 47

Question on R Shiny gets table click information from lineups

I am trying to get the table row information (such as row name. number, or cell value) when I click the lineup table, but I don;t know how. Can someonle help with it? Any input will be greatly appreciated.

The following is my sample code to show the problem

library(shiny)
library(crosstalk)
library(lineupjs)
library(magrittr)

ui <- fluidPage(
  splitLayout(
    column(12,
        lineupOutput("table1", height=300),
        textInput("val1", "Value1", "Tobeupdated1")
    ),
    column(12,
        DT::dataTableOutput("table2"),
        textInput("val2", "Value2", "Tobeupdated2")
    )
  )
)

server <- function(input, output,session) {
  dfshow <- head(iris,n=5)

  shared_iris <- SharedData$new(dfshow)
 t<-1

  output$table1 <- renderLineup({
    lineup(shared_iris)
  })

  output$table2<- DT::renderDT(dfshow, selection = 'single', rownames = FALSE,  editable = TRUE)

  observeEvent(input$table1_rows_selected,{
    rowIndex <- input$table1_rows_selected
    updateTextInput(session, "val1", value = as.character(input$table1_rows_selected))
  })

  observeEvent(input$table2_rows_selected,{
    rowIndex <- input$table2_rows_selected
    updateTextInput(session, "val2", value = as.character(input$table2_rows_selected))
  })

}
shinyApp(ui = ui, server = server)

What I want is when I click the lineup table1, I want to update the value below the table1, like as the one on the right. Thanks.enter image description here

Upvotes: 2

Views: 133

Answers (1)

lz100
lz100

Reputation: 7330

It's hidden in the shared_iris$selection() method.

Do something like this:

library(shiny)
library(crosstalk)
library(lineupjs)
library(magrittr)

ui <- fluidPage(
    splitLayout(
        column(12,
               lineupOutput("table1", height=300),
               textInput("val1", "Value1", "Tobeupdated1")
        ),
        column(12,
               DT::dataTableOutput("table2"),
               textInput("val2", "Value2", "Tobeupdated2")
        )
    )
)

server <- function(input, output,session) {
    dfshow <- head(iris,n=5)
    
    shared_iris <- SharedData$new(dfshow)
    t<-1
    
    output$table1 <- renderLineup({
        lineup(shared_iris)
    })

    output$table2<- DT::renderDT(dfshow, selection = 'single', rownames = FALSE,  editable = TRUE)
    
    observeEvent(shared_iris$selection(),{
        rowIndex <- which(shared_iris$selection())
        updateTextInput(session, "val1", value = as.character(rowIndex))
    })
    
    observeEvent(input$table2_rows_selected,{
        rowIndex <- input$table2_rows_selected
        updateTextInput(session, "val2", value = as.character(input$table2_rows_selected))
    })
    
}
shinyApp(ui = ui, server = server)

enter image description here

Upvotes: 1

Related Questions