javascript - Shiny DataTable Callback - Stack Overflow

I am a little bit of a javascript novice so I am having trouble getting a function in DataTables callba

I am a little bit of a javascript novice so I am having trouble getting a function in DataTables callback option to work.

In this small example, I want to write a javascript function so that when a user selects a row in the mtcars datatable, if the value of MPG is greater than 20, for their to be an alert that says "Good for you!". It is similar to this, but that example doesn't use shiny. Here is what I tried:

library(shiny)
library(DT)
server <- function(input, output) {
    output$one <- DT::renderDataTable(mtcars,options=list(callback=DT::JS(
      'function(table) {
      table.on("click.dt","tr", function() {
      var data=table.row(this).data();
      if (parseFloat(data[0]>20.0))      
        alert("Good for you!");
      });}'    
      )))
}

ui <- fluidPage(mainPanel(DT::dataTableOutput("one")))
shinyApp(ui = ui, server = server) 

Selecting rows with MPG greater than 20 does not produce the alert like I want. I feel like I may be fundamentally misunderstanding how the javascript works int he callback option. Any help would be appreciated.

Regards

I am a little bit of a javascript novice so I am having trouble getting a function in DataTables callback option to work.

In this small example, I want to write a javascript function so that when a user selects a row in the mtcars datatable, if the value of MPG is greater than 20, for their to be an alert that says "Good for you!". It is similar to this, but that example doesn't use shiny. Here is what I tried:

library(shiny)
library(DT)
server <- function(input, output) {
    output$one <- DT::renderDataTable(mtcars,options=list(callback=DT::JS(
      'function(table) {
      table.on("click.dt","tr", function() {
      var data=table.row(this).data();
      if (parseFloat(data[0]>20.0))      
        alert("Good for you!");
      });}'    
      )))
}

ui <- fluidPage(mainPanel(DT::dataTableOutput("one")))
shinyApp(ui = ui, server = server) 

Selecting rows with MPG greater than 20 does not produce the alert like I want. I feel like I may be fundamentally misunderstanding how the javascript works int he callback option. Any help would be appreciated.

Regards

Share Improve this question asked Feb 25, 2016 at 17:32 CarlCarl 5,7797 gold badges45 silver badges83 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You just need the body of the callback function in the callback argument of renderDataTable (you don't need options here:

    server <- function(input, output) {
        output$one <- DT::renderDataTable(mtcars,callback=JS(
                'table.on("click.dt","tr", function() {
                var data=table.row(this).data();
                if (parseFloat(data[1])>20.0)      
                 alert("Good for you!");
})'    
      ))
}

Your parseFloat was also around the whole if condition, and data[0] is the name of the cars, data[1] will be the mpg.

You can troubleshoot this by using developer tools and console.log in your javascript to print to the console. You could for example add console.log(data) before your if and check what it looks like.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745439016a4627745.html

相关推荐

  • javascript - Shiny DataTable Callback - Stack Overflow

    I am a little bit of a javascript novice so I am having trouble getting a function in DataTables callba

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信