使用数据表单单元格选择在R Shiny中子集数据帧

时间:2018-11-28 23:52:11

标签: r shiny dt

我下面有一个闪亮的应用程序,在该应用程序中,我希望能够单击单个列,然后仅使用此单元格创建第二个数据表。问题是我无法通过行选择来实现这一点,因为行选择会返回用于子集的行索引。在这种情况下,我以第一行-3rd列为例。

library(shiny)

fluidPage(

  title = 'Select Table Rows',


  h1('A Server-side Table'),

  fluidRow(
    column(9, DT::dataTableOutput('x3')),
    column(3, DT::dataTableOutput('x4'))
  )

)
#server.r
   library(shiny)
library(DT)

shinyServer(function(input, output, session) {

  # server-side processing
  mtcars2 = mtcars[, 1:8]
  output$x3 = DT::renderDataTable(mtcars2, server = TRUE,selection=list(target='cell'))

  # print the selected indices
  output$x4 = DT::renderDataTable({
    if(as.character(input$x3_cells_selected)=="1, 3"){
      mtcars[1,3]
    }

  })

})

1 个答案:

答案 0 :(得分:1)

这就是您需要的-

library(shiny)
library(DT)

ui <- fluidPage(
  title = 'Select Table Rows',
  h1('A Server-side Table'),
  fluidRow(
    column(9, DT::dataTableOutput('x3')),
    column(3, DT::dataTableOutput('x4'))
  )
)
#server.r
server <- function(input, output, session) {

  # server-side processing
  mtcars2 = mtcars[, 1:8]
  output$x3 = DT::renderDataTable(mtcars2, server = TRUE,
                                  selection=list(target='cell'))

  # print the selected indices
  output$x4 = DT::renderDataTable({
    req(input$x3_cells_selected)
    mtcars[input$x3_cells_selected[,1], input$x3_cells_selected[,2], drop = F]
  })
}

shinyApp(ui, server)