只需将坐标添加到相应的
row
和
col
值中即可。获取用于创建数据表的
table
,然后获取
input$dt_cell_clicked$row
和
$col
,并请求
table[input$dt_cell_clicked$row + 1, input$dt_cell_clicked$col]
或反之亦然。示例应用程序:
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:"))
})
output$shifted_cell <- renderPrint({
mtcars[input$dt_cell_clicked$row + input$y,
input$dt_cell_clicked$col + input$x]
})
output$dt <- DT::renderDataTable({
DT::datatable(mtcars, select="none")})
}
shinyApp(ui, server)
![enter image description here](https://istack.dev59.com/X3xLj.webp)