获取渲染的DT表的列名

时间:2018-10-18 09:01:11

标签: r shiny dt

我接手了一个项目,该项目在使用DT::renderDT()渲染之前使用了一些整洁的功能来删除空/丑陋的列。给定列中的单元格值可能代表另一个表,我想链接到这些表。因此,如果用户单击一个单元格,则应用程序应呈现该名称的另一个表。但是,单元格值仅在列名的上下文中与其他表唯一相关。input$tbl_cell_clicked仅提供索引,而不提供列名。整理功能可能会删除空列,因此我不能依赖索引号。

如何获取表的当前呈现列名称?

  library(shiny)
  library(DT)
  shinyApp(
    ui = fluidPage(fluidRow(column(12, DTOutput('tbl')))),
    server = function(input, output) {
      output$tbl = renderDT(

        #I took over a project that uses some tidy functions to drop empty/ugly columns before rendering

        #column names and order therefore cannot be guranteed, here exemplified with use of sample()
        iris[,sample(ncol(iris),3)], options = list(lengthChange = FALSE)
      )

      #i need to know the cell value and column name of latest click gesture, not only index col
      observe({

        #how can I retrieve the column name that cell clicked was in?
        print(input$tbl_cell_clicked)

        #The rendered table iris[,sample(ncol(iris))] cannot be scoped from here

        #Don't wanna go down that road of <<- solutions
        #could be solved by dumping latest iris[,sample(ncol(iris),3)] into a reactive values,
        #but that might look messy to use extra lines to save metadata from latest rendered DT


      })
    }
  )

2 个答案:

答案 0 :(得分:1)

这是一个JavaScript解决方案。

library(shiny)
library(DT)

js <- c(
  "table.on('click', 'td', function(){",
  "  var cell = table.cell(this);",
  "  var colindex = cell.index().column;",
  "  var colname = table.column(colindex).header().innerText;",
  "  Shiny.setInputValue('column_clicked', colname);",
  "});"
)

shinyApp(
  ui = fluidPage(
    fluidRow(column(12), verbatimTextOutput("colclicked")),
    fluidRow(column(12, DTOutput('tbl')))
  ),

  server = function(input, output) {

    output$tbl = renderDT(
      iris[,sample(ncol(iris),3)], options = list(lengthChange = FALSE), 
      callback = JS(js)
    )

    output$colclicked <- renderPrint({
      input[["column_clicked"]]
    })
  }
)

enter image description here

答案 1 :(得分:0)

可以将无功值参考插入整齐的函数中。整洁的函数将在转储为包含列名的最新值的反应值之前转储信息。

#some tidy function mentioned several times many different place in code
a_tidy_function = function(
  dt, #df or dt
  #added this argument to tidy function
  reactiveRef=NULL) {
  #tidy data.frame / data.table
  tidy_dt = dt[,sample(ncol(dt))]

  #include this line to update reactive value at reactiveRef
  if(!is.null(reactiveRef)) reactiveRef(colnames(tidy_dt)) #

  return(tidy_dt)
}

  library(shiny)
  library(DT)
  shinyApp(
    ui = fluidPage(fluidRow(column(12, DTOutput('tbl')))),
    server = function(input, output) {

      r_latest_column = reactiveVal()
      output$tbl = renderDT(

        #I took over a project that uses some tidy functions to drop empty/ugly columns before rendering

        #column names and order therefore cannot be guranteed, here exemplified with use of sample()
        {


        iris_rendered = a_tidy_function(
            iris,
            reactiveRef = r_latest_column) #col name info dumped to r_latest_column
        iris_rendered
        }, options = list(lengthChange = FALSE)

      )

      #i need to know the cell value and column name of latest click gesture, not only index col
      observe({

        #here are the value and indexes
        print(input$tbl_cell_clicked)

        #and... column names are now accesible here...
        print(r_latest_column())


      })
    }
  )

正如@hrbrmstr在评论中提到的,可以从javascript域中检索列名,我尝试了这一点,对于今天我必须完成的工作来说似乎太麻烦了,但是我发现我将研究这个有希望的教程... < / p>

https://github.com/FrissAnalytics/shinyJsTutorials https://github.com/FrissAnalytics/shinyJsTutorials/tree/master/tutorials/materials3/messageApp2