[英]DT shiny R tooltip and hide column
I'm trying to do two things on a DT data table in shiny R. 我正在尝试在闪亮的R中的DT数据表上做两件事。
My code (example from github) is the following: 我的代码(来自github的示例)如下:
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");
}')
)
)
})
}
)
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? 是否可以隐藏第4列? I just want to evaluate it, I don't want it to be shown. 我只想评估它,不希望它显示出来。
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专家。
Thank you in advance! 先感谢您!
Yes it is possible to add a tool tip only to the cells with green background. 是的,可以仅向具有绿色背景的单元格添加工具提示。 We have to use javascript below: 我们必须在下面使用javascript:
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);",
"}",
"}")
)
)
[EDIT]: [编辑]:
To add formattings to the tooltip I have added few more lines and it only works in shinyApp and not DT datatable. 为了向工具提示添加格式,我增加了几行,它仅适用于shinyApp,而不适用于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,",
"});",
"}",
"}")
)
)
})
}
)
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.