R 获取呈现的DT表的列名
我接管了一个项目,在使用R 获取呈现的DT表的列名,r,shiny,dt,R,Shiny,Dt,我接管了一个项目,在使用DT::renderDT()进行渲染之前,它使用一些整洁的函数删除空的/丑陋的列。给定列中的单元格值可能表示另一个表,我想链接到这些表。因此,如果用户单击某个单元格,应用程序应呈现该名称的另一个表。但是,单元格值仅与列名上下文中的其他表唯一相关。input$tbl\u cell\u clicked仅提供索引,而不提供列名。整理函数可能会删除空列,因此我不能依赖索引号 如何获取表的当前呈现列名 library(shiny) library(DT) shinyAp
DT::renderDT()
进行渲染之前,它使用一些整洁的函数删除空的/丑陋的列。给定列中的单元格值可能表示另一个表,我想链接到这些表。因此,如果用户单击某个单元格,应用程序应呈现该名称的另一个表。但是,单元格值仅与列名上下文中的其他表唯一相关。input$tbl\u cell\u 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
})
}
)
库(闪亮)
图书馆(DT)
shinyApp(
ui=fluidPage(fluidRow(第12列,DTOutput('tbl'))),
服务器=功能(输入、输出){
输出$tbl=renderDT(
#我接管了一个项目,该项目使用一些整洁的函数在渲染之前删除空/丑陋的列
#因此,无法保证列名和顺序,这里使用sample()作为示例
iris[,样本(ncol(iris),3)],选项=列表(长度变化=假)
)
#我需要知道最新点击手势的单元格值和列名,而不仅仅是索引列
观察({
#如何检索单击的单元格所在的列名?
打印(输入$tbl\u单元格\u单击)
#呈现的表iris[,sample(ncol(iris))]不能从此处起作用
#不想走那种被动值引用可以插入到tidy函数中的道路。tidy函数在渲染到包含列名的被动值之前转储信息最新渲染的数据
#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域检索列名,我尝试过了,但对于我今天必须完成的工作来说,这似乎太麻烦了,不过我发现这本有前途的教程我将研究
可以在tidy函数中插入反应值引用。tidy函数在渲染为包含列名的反应值(最新渲染数据)之前会转储信息
#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域检索列名,我尝试过了,但对于我今天必须完成的工作来说,这似乎太麻烦了,不过我发现这本有前途的教程我将研究
这里是一个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"]]
})
}
)
库(闪亮)
图书馆(DT)
js这里是一个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"]]
})
}
)
库(闪亮)
图书馆(DT)
js看一下--但它意味着使用创建的javascript变量并将这些信息返回到R中。看一下--但它意味着使用创建的javascript变量并将这些信息返回到R中。