在R Shiny

时间:2017-01-06 08:05:04

标签: javascript r shiny dt

我正在尝试使用JS脚本 attr 函数添加工具提示,一些书面脚本没有给出所需的结果(为数据表的第一列添加工具提示)。我是新的JS脚本因此无法调试错误,任何人都可以建议我为什么下面的代码没有给我正确的结果。这是一段代码:

library(shiny)
library(DT)

shinyApp(
ui = fluidPage(

DT::dataTableOutput("table2")

),
server = function(input, output) {

output$table2<-DT::renderDataTable({
  responseDataFilter2_home<-iris[,c(4,3,1)]
  displayableData<-as.data.frame(responseDataFilter2_home, stringAsFactors = FALSe, row.names = NULL)
},server = TRUE, selection = 'single',callback = JS("table.on('dblclick.dt', 'td', function(nRow, aData){
                                                         var row=table.cell(this).index().row;
                                                        var full_text = aData[1] + ','+ aData[2]
                                                        $('td:eq(1)', nRow).attr('title', full_text);
                                                         Shiny.onInputChange('rows_home',[row, Math.random()]);});
                                                         table.on('click.dt', 'td', function(nRow, aData) {
                                                         var k=table.cell(this).index().row;
                                                        var full_text = aData[1] + ','+ aData[2]
                                                        $('td:eq(1)', nRow).attr('title', full_text);
                                                         if(table.rows('.selected').indexes().toArray()!= '' && table.rows('.selected').indexes().toArray() ==k){
                                                         k=-1;}
                                                         Shiny.onInputChange('rows_up_home',[k, Math.random()]);
                                                         });"),
escape=FALSE,options=list(paging=FALSE,searching = FALSE,ordering=FALSE,scrollY = 400,scrollCollapse=TRUE,
                          columnDefs = list(list(width = '800%', targets = c(1)))),rownames=FALSE,colnames="Name")

}
)

2 个答案:

答案 0 :(得分:2)

我已使用我在此link中给出的答案修改了您的代码,以便您在不影响JS代码的其他部分的情况下获得工具提示:

library(shiny)
library(DT)

shinyApp(
  ui = fluidPage(

    DT::dataTableOutput("table2")

  ),
  server = function(input, output) {

    output$table2<-DT::renderDataTable({
      responseDataFilter2_home<-iris[,c(4,3,1)]
      displayableData<-DT::datatable(data = as.data.frame(responseDataFilter2_home, stringAsFactors = FALSE, row.names = NULL),rownames = FALSE,
                                     escape = FALSE, selection = 'single', callback =  JS("table.on('dblclick.dt', 'td', function(nRow, aData){
                                                         var row=table.cell(this).index().row;
                                                        var full_text = aData[1] + ','+ aData[2]
                                                        $('td:eq(1)', nRow).attr('title', full_text);
                                                        Shiny.onInputChange('rows_home',[row, Math.random()]);});
                                                        table.on('click.dt', 'td', function(nRow, aData) {
                                                        var k=table.cell(this).index().row;
                                                        var full_text = aData[1] + ','+ aData[2]
                                                        $('td:eq(1)', nRow).attr('title', full_text);
                                                        if(table.rows('.selected').indexes().toArray()!= '' && table.rows('.selected').indexes().toArray() ==k){
                                                        k=-1;}
                                                        Shiny.onInputChange('rows_up_home',[k, Math.random()]);
                                                        });"),

                                     options = list(rowCallback = JS(
                                       "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                                       "var full_text = aData[0] + ','+ aData[1];",
                                       "$('td:eq(0)', nRow).attr('title', full_text);",
                                       "}"),paging=FALSE,searching = FALSE,ordering=FALSE,scrollY = 400,scrollCollapse=TRUE,
                                       columnDefs = list(list(width = '800%', targets = c(1))),colnames="Name") )


    })

    })

希望它有所帮助!

答案 1 :(得分:0)

您可以从这个最小的工作示例开始:

library(shiny)
library(DT)

shinyApp(
  ui = fluidPage(

    DT::dataTableOutput("table2")

  ),
  server = function(input, output) {

    output$table2<-DT::renderDataTable({
      responseDataFilter2_home<-iris[,c(4,3,1)]
      displayableData<-DT::datatable(responseDataFilter2_home,options = list(rowCallback = JS(
        "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
        "var full_text = aData[1] + ','+ aData[2]",
        "$('td:eq(1)', nRow).attr('title', full_text);",
        "}")
      ))#, stringAsFactors = FALSe, row.names = NULL)
    },server = TRUE, selection = 'single', escape=FALSE,options=list(paging=FALSE,searching = FALSE,ordering=FALSE,scrollY = 400,scrollCollapse=TRUE,
                          columnDefs = list(list(width = '800%', targets = c(1)))),rownames=FALSE,colnames="Name")

    }
 )

enter image description here