简体   繁体   English

在Shiny DT中单击单元格旁边获取单元格值

[英]Get cell value next to clicked cell in Shiny DT

My Shiny App renders DT frame in DT aka DataTables . My Shiny App在DT aka DataTables渲染DT帧。 I know how to get value of clicked cell by adding suffix: 我知道如何通过添加后缀获得点击单元格的值:

_cell_clicked

For example: 例如:

> print(unlist(( input$renderMpaDtOutput_cell_clicked )))

returns named list object: 返回命名列表对象:

row col value 1 9 3929

But I want to get cell value next to clicked cell (for example next to above coordinates: (row,col) = (1,9) ). 但是我希望在单击的单元格旁边获取单元格值(例如,在上面的坐标旁边: (row,col) = (1,9) )。

Any ideas? 有任何想法吗?

Just by adding coordinates to the row and col value, respectively. 只需将坐标分别添加到rowcol值即可。 Take the table that was used to create the datatable, get input$dt_cell_clicked$row and $col and ask for table[input$dt_cell_clicked$row + 1, input$dt_cell_clicked$col] or vice versa. 获取用于创建数据table ,获取input$dt_cell_clicked$row$col并询问table[input$dt_cell_clicked$row + 1, input$dt_cell_clicked$col] ,反之亦然。 Example app: 应用示例:

library(shiny)

ui <- fluidPage(
 numericInput("x", "how many cells to the left/right?", min=-5, max=5, value=0),
 numericInput("y", "how many cells to the top/bottom?", min=-5, max=5, value=0),
 DT::dataTableOutput("dt"),
 uiOutput("headline"),
 verbatimTextOutput("shifted_cell")
)

server <- function(input, output) {

  output$headline <- renderUI({
    h3(paste0("You clicked value ", input$dt_cell_clicked$value, ". ", 
              input$x, " cells to the ", ifelse(input$x > 0, "right", "left"), " and ",
              input$y, " cells to the ", ifelse(input$y > 0, "bottom", "top"), " is:"))
  })
  # the value of the shifted cell
  output$shifted_cell <- renderPrint({
    mtcars[input$dt_cell_clicked$row + input$y, # clicked plus y to the bottom/top
           input$dt_cell_clicked$col + input$x] # clicked plus x to the left/right 
  })

  # the datatable
  output$dt <- DT::renderDataTable({
    DT::datatable(mtcars, select="none")})
}

shinyApp(ui, server)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM