KmnsE2
KmnsE2

Reputation: 424

How to access the contents of a selected DT line?

library(shiny)
library(DT)
ui <- basicPage(
  mainPanel(DT::dataTableOutput('row_modif')),
  textOutput("selectedrow")
)

server <- function(input, output, session){
  
  output$row_modif<-renderDT({
    
    datatable(
      iris, selection = "single") 
    
  })
    output$selectedrow<-renderPrint({input$row_modif_rows_selected})

} 
shinyApp(ui, server)

it is possible to access the number of the selected line by:

input$row_modif_rows_selected


But how i can acess one specific column from this line selected? the idea is to click on a row and access the species and Sepal.Length column of that row for example

Upvotes: 3

Views: 155

Answers (1)

DaveArmstrong
DaveArmstrong

Reputation: 21927

You need to use input$row_modif_rows_selected to index the rows of the iris dataset. Note that input$row_modif_rows_selected just returns the row number, not the row itself.

library(shiny)
library(DT)
ui <- basicPage(
  mainPanel(
    DT::dataTableOutput('row_modif'),
    tags$p("Selected Row:\n"),
    textOutput("selectedrow"), 
    tags$p("Species and Sepal Length for Selected Row:\n"), 
    DT::dataTableOutput("sscols")
  )
)
server <- function(input, output, session){
  
  output$row_modif<-renderDT({
    
    datatable(
      iris, selection = "single") 
    
  })
  output$selectedrow<-renderPrint({input$row_modif_rows_selected})
  output$sscols <- renderDT({
    datatable(iris[input$row_modif_rows_selected, c("Species", "Sepal.Length")])
  })
} 
shinyApp(ui, server)

Upvotes: 2

Related Questions