Get Selected Row From DataTable in Shiny App

前端 未结 3 1670
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 01:57

I want to modify this application:

https://demo.shinyapps.io/029-row-selection/

so that only one row can be selected at a time, and so that I can acquire the

3条回答
  •  自闭症患者
    2020-12-14 02:30

    The below code display a dataframe in DT table format. Users will be able to select single row. The selected row is retrieved and displayed. you can write your plot function in the plot block in the server.

    I hope this helps !!

             # Server.R
             shinyServer(function(input, output,session) {
    
    
    
    
              output$sampletable <- DT::renderDataTable({
              sampletable
              }, server = TRUE,selection = 'single')  
    
              output$selectedrow <- DT::renderDataTable({
    
              selectedrowindex <<-     input$sampletable_rows_selected[length(input$sampletable_rows_selected)]
             selectedrowindex <<- as.numeric(selectedrowindex)
             selectedrow <- (sampletable[selectedrowindex,])
             selectedrow
    
    
    
               })
    
              output$plots <- renderPlot({
    
              variable <- sampletable[selectedrowindex,1]
              #write your plot function
    
    
                  })
    
    
              })
    
              #ui.R 
              shinyUI(navbarPage( "Single Row Selection",
    
    
    
                    tabPanel("Row selection example",
                             sidebarLayout(sidebarPanel("Parameters"),
                                 mainPanel(
                                   DT::dataTableOutput("selectedrow"),   
                                 DT::dataTableOutput("sampletable")
    
                               ))
    
                          )
    
                          ))
    
             # global.R 
    
            library(DT)
            library(shiny)
            selectedrowindex = 0
    

提交回复
热议问题