kgui
kgui

Reputation: 4163

Retrieving rows from DataTable Shiny

I would like to save the row that was selected as an array or some other indexable object. But, I just don't seem to get how to do this.

Here is the code. The line giving me trouble is output$rowData <- renderText({input$table1[s]})

fluidPage(

   title = " Nanoproject",

   h1("Peak Table and Views"),

   # first row will contain the peak table and peak view 
   fluidRow(
   # give the table half of the page
      column(6, dataTableOutput('table1'), height = 2500),

   #give the other half of the page to the images
      column(6 , 
       imageOutput("image1",width = "auto",height = "auto"),
       htmlOutput("rowData")
   )

server.R

# prompt user to select the peak table file
writeLines("Please select the peaks file")
peaksPath = file.choose()

# read the tsv file 
peaksTable = read.csv(peaksPath , header = TRUE , row.names = NULL , sep = "\t")

server = function(input, output) {
    output$table1 = renderDataTable({
    # the peak table 
    datatable(peaksTable,
          # when rowname is false each row does not have a numeric # associated with it 
          rownames = TRUE,
          # specify the name of the column headers
          colnames = c("Seqnames", "Start", "End","Width","Strand","P","Q","Effectsize",
                       "FDR","Keep","Gene_name","Gene.nearest","Count","Count.pred",
                       "Coverage","Local.mut.density","Base.context.GC","Tn.Context.TpC",
                       "Tn.context.CpG","Dnase","Activechrom","Hetchrom","Rept"),
          # make the table scrollable (horizontally)
          options = list(scrollX = TRUE))

},
escape = FALSE)

output$image1 <- renderImage({

# save which row was selected
s = input$table1_rows_selected

# output the data of the row that was selected
output$rowData <- renderText({input$table1[s]})

#output$rowsSelected <- renderText({s})
list(src=paste0(imagePath,"/peak" , s,".png"),width=700,height=2500)},deleteFile=FALSE)

Upvotes: 2

Views: 1188

Answers (1)

NicE
NicE

Reputation: 21443

input$tableId_rows_selected returns the indices of the rows of the table that are selected.

In your case, you should subset peakTable to get the data of the selected rows.

You could try:

output$rowData <- renderText({peakTable[s,]})

input$table1 does not hold the table you display in output$table1

Upvotes: 2

Related Questions