DT闪亮R工具提示和隐藏列 [英] DT shiny R tooltip and hide column
问题描述
我正在尝试在闪亮的R中的DT数据表上做两件事。
I'm trying to do two things on a DT data table in shiny R.
我的代码(来自github的示例)如下:
My code (example from github) is the following:
library("shiny")
library("shinydashboard")
library("datasets")
library("DT")
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(
DT::dataTableOutput("mtcarsTable")
)
shinyApp(
ui = dashboardPage(header, sidebar, body),
server = function(input, output) {
output$mtcarsTable <- renderDataTable({
DT::datatable(datasets::mtcars,
options = list(rowCallback = JS('
function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
// Bold and green cells for conditions
if (parseFloat(aData[4]) >= 200)
$("td:eq(3)", nRow).css("font-weight", "bold");
if (parseFloat(aData[4]) >= 100)
$("td:eq(3)", nRow).css("background-color", "#9BF59B");
}')
)
)
})
}
)
如您所见,我正在评估第4列,为单元格提供背景色以及定义其是否应为粗体。
As you can see I'm evaluating column 4, to give to a cell a background color as also as to define if it should be bold or not.
是否可以隐藏第4列?我只想评估它,我不希望它显示出来。
Is it possible to hide column 4? I just want to evaluate it, I don't want it to be shown.
我的另一个问题是,是否有可能仅向具有绿色背景?我看到我应该使用回调函数,但我不知道该怎么做,而且我不是javascript方面的专家。
My other question is if it is possible to add a tool tip only to the cells with green background? I saw that I should use the callback but I don't know how and I'm not an expert at javascript.
谢谢!
推荐答案
是的,可以仅将工具提示添加到绿色背景的单元格中。我们必须在下面使用javascript:
Yes it is possible to add a tool tip only to the cells with green background. We have to use javascript below:
DT::datatable(datasets::mtcars,
options = list(rowCallback = JS(
"function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
"// Bold and green cells for conditions",
"if (parseFloat(aData[4]) >= 200)",
"$('td:eq(3)', nRow).css('font-weight', 'bold');",
"if (parseFloat(aData[4]) >= 100){",
"$('td:eq(3)', nRow).css('background-color', '#9BF59B');",
"var full_text = aData[3]",
"$('td:eq(3)', nRow).attr('title', full_text);",
"}",
"}")
)
)
:
要在工具提示中添加格式,我还添加了几行,仅适用于ShinyApp,不适用于DT数据表。参见下面的代码:
To add formattings to the tooltip I have added few more lines and it only works in shinyApp and not DT datatable. See the code below:
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(
DT::dataTableOutput("mtcarsTable")
)
shinyApp(
ui = dashboardPage(header, sidebar, body),
server = function(input, output) {
output$mtcarsTable <- renderDataTable({
DT::datatable(datasets::mtcars,
options = list(rowCallback = JS(
"function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
"// Bold and green cells for conditions",
"if (parseFloat(aData[4]) >= 200)",
"$('td:eq(3)', nRow).css('font-weight', 'bold');",
"if (parseFloat(aData[4]) >= 100){",
"$('td:eq(3)', nRow).css('background-color', '#9BF59B');",
"var full_text = aData[3]",
"$('td:eq(3)', nRow).attr('title', full_text);",
"//Code for formatting tooltip",
"$('td:eq(3)', nRow).tooltip({",
"'delay': 0,",
"'track': true,",
"'fade': 250,",
"});",
"}",
"}")
)
)
})
}
)
希望这会有所帮助。
这篇关于DT闪亮R工具提示和隐藏列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!