【问题标题】:Print the value of a selected cell from a datatable打印数据表中选定单元格的值
【发布时间】:2019-10-29 20:20:09
【问题描述】:

我想在单击时提取选定单元格的值而不是其行和列坐标,因为我想将其用作另一个过程的输入。

library(shiny)
library(DT)
data("mtcars")

ui <- shinyUI(
  fluidRow(
    DT::dataTableOutput("myDatatable"),
    verbatimTextOutput("selectedCells")
  )
)

server <- shinyServer(function(input, output, session) {
  output$myDatatable <- DT::renderDataTable(mtcars, 
                                            selection=list(mode="single", target="cell"),
                                            server = FALSE,
                                            rownames=FALSE)

  output$selectedCells <- renderPrint(input$myDatatable_cells_selected)
})

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    您可以使用如下所示的行号和列号访问表中的值:

    library(shiny)
    library(DT)
    data("mtcars")
    
    ui <- shinyUI(fluidRow(
        DT::dataTableOutput("myDatatable"),
        verbatimTextOutput("selectedCells")
    ))
    
    server <- shinyServer(function(input, output, session) {
        output$myDatatable <- DT::renderDataTable(
            mtcars,
            selection = list(mode = "single", target =
                                 "cell"),
            server = FALSE,
            rownames = FALSE
        )
    
        output$selectedCells <- renderPrint({
            s = input$myDatatable_cells_selected
            if (!is.null(s) && ncol(s) != 0) {
                mtcars[s[1, 1] , s[1, 2] + 1]
            } else {
                NULL
            }
        })
    })
    
    shinyApp(ui, server)
    

    如您所见,必须向列值添加一个以指定适当的位置。处理未选中的情况也很重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 2016-08-12
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多