Is there any way to add custom R-function action to a button in DT?












1















I'm trying to add a custom button performing the action defined by R-function to my datatable. I've used the same list of options in my R code as in Javascript code in Datatables manual, but it doesn't work.



Here is a code from Datatables manual:



$(document).ready(function() {
$('#example').DataTable( {
dom: 'Bfrtip',
buttons: [
{
text: 'My button',
action: function ( e, dt, node, config ) {
alert( 'Button activated' );
}
}
]
} );
} );


And here is my code in R:



require(DT)
DT::datatable(iris,
extensions = 'Buttons',
options = list(
dom = 'Bfrtip',
buttons = list(
list(
text = 'test',
action = print('1')
)
)
)
)


Executing it I've received an error:



Error in if (extend != "collection") extend else listButtons(cfg) : 
argument is of length zero









share|improve this question





























    1















    I'm trying to add a custom button performing the action defined by R-function to my datatable. I've used the same list of options in my R code as in Javascript code in Datatables manual, but it doesn't work.



    Here is a code from Datatables manual:



    $(document).ready(function() {
    $('#example').DataTable( {
    dom: 'Bfrtip',
    buttons: [
    {
    text: 'My button',
    action: function ( e, dt, node, config ) {
    alert( 'Button activated' );
    }
    }
    ]
    } );
    } );


    And here is my code in R:



    require(DT)
    DT::datatable(iris,
    extensions = 'Buttons',
    options = list(
    dom = 'Bfrtip',
    buttons = list(
    list(
    text = 'test',
    action = print('1')
    )
    )
    )
    )


    Executing it I've received an error:



    Error in if (extend != "collection") extend else listButtons(cfg) : 
    argument is of length zero









    share|improve this question



























      1












      1








      1








      I'm trying to add a custom button performing the action defined by R-function to my datatable. I've used the same list of options in my R code as in Javascript code in Datatables manual, but it doesn't work.



      Here is a code from Datatables manual:



      $(document).ready(function() {
      $('#example').DataTable( {
      dom: 'Bfrtip',
      buttons: [
      {
      text: 'My button',
      action: function ( e, dt, node, config ) {
      alert( 'Button activated' );
      }
      }
      ]
      } );
      } );


      And here is my code in R:



      require(DT)
      DT::datatable(iris,
      extensions = 'Buttons',
      options = list(
      dom = 'Bfrtip',
      buttons = list(
      list(
      text = 'test',
      action = print('1')
      )
      )
      )
      )


      Executing it I've received an error:



      Error in if (extend != "collection") extend else listButtons(cfg) : 
      argument is of length zero









      share|improve this question
















      I'm trying to add a custom button performing the action defined by R-function to my datatable. I've used the same list of options in my R code as in Javascript code in Datatables manual, but it doesn't work.



      Here is a code from Datatables manual:



      $(document).ready(function() {
      $('#example').DataTable( {
      dom: 'Bfrtip',
      buttons: [
      {
      text: 'My button',
      action: function ( e, dt, node, config ) {
      alert( 'Button activated' );
      }
      }
      ]
      } );
      } );


      And here is my code in R:



      require(DT)
      DT::datatable(iris,
      extensions = 'Buttons',
      options = list(
      dom = 'Bfrtip',
      buttons = list(
      list(
      text = 'test',
      action = print('1')
      )
      )
      )
      )


      Executing it I've received an error:



      Error in if (extend != "collection") extend else listButtons(cfg) : 
      argument is of length zero






      r datatables dt






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 13:48







      asvx

















      asked Nov 17 '18 at 13:27









      asvxasvx

      1515




      1515
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You have to set extend = "collection", like this:



          library(DT)
          datatable(iris,
          extensions = 'Buttons',
          options = list(
          dom = 'Bfrtip',
          buttons = list(
          "copy",
          list(
          extend = "collection",
          text = 'test',
          action = DT::JS("function ( e, dt, node, config ) {
          alert( 'Button activated' );
          }")
          )
          )
          )
          )


          But the action can only execute some Javascript, not a R command. However you can execute a R command by clicking a custom button if you put the datatable in a shiny application. Something like that:



          library(shiny)
          library(DT)

          ui <- basicPage(
          DTOutput("dtable")
          )

          server <- function(input, output, session){
          output$dtable <- renderDT(
          datatable(iris,
          extensions = 'Buttons',
          options = list(
          dom = 'Bfrtip',
          buttons = list(
          "copy",
          list(
          extend = "collection",
          text = 'test',
          action = DT::JS("function ( e, dt, node, config ) {
          Shiny.setInputValue('test', true);
          }")
          )
          )
          )
          )
          )

          observeEvent(input$test, {
          if(input$test){
          print("hello")
          }
          })
          }

          shinyApp(ui, server)





          share|improve this answer


























          • That works, but I’ve changed your code slightly in order to use the button as a trigger. So I’ve added priority: "event" option as it was described in the section 'Values vs. Events' of this Shiny manual page.

            – asvx
            Nov 18 '18 at 11:51











          • Could you clarify why did you use if(input$test)? The code works fine for me without it.

            – asvx
            Nov 18 '18 at 11:58











          • @asvx You're right, the if is useless. And you're right to use priority: "event". I didn't know priority: "event", I used a trick before to trigger the input change when the value does not change, but using priority: "event" is definitely better. Thanks.

            – Stéphane Laurent
            Nov 19 '18 at 9:34











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351684%2fis-there-any-way-to-add-custom-r-function-action-to-a-button-in-dt%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You have to set extend = "collection", like this:



          library(DT)
          datatable(iris,
          extensions = 'Buttons',
          options = list(
          dom = 'Bfrtip',
          buttons = list(
          "copy",
          list(
          extend = "collection",
          text = 'test',
          action = DT::JS("function ( e, dt, node, config ) {
          alert( 'Button activated' );
          }")
          )
          )
          )
          )


          But the action can only execute some Javascript, not a R command. However you can execute a R command by clicking a custom button if you put the datatable in a shiny application. Something like that:



          library(shiny)
          library(DT)

          ui <- basicPage(
          DTOutput("dtable")
          )

          server <- function(input, output, session){
          output$dtable <- renderDT(
          datatable(iris,
          extensions = 'Buttons',
          options = list(
          dom = 'Bfrtip',
          buttons = list(
          "copy",
          list(
          extend = "collection",
          text = 'test',
          action = DT::JS("function ( e, dt, node, config ) {
          Shiny.setInputValue('test', true);
          }")
          )
          )
          )
          )
          )

          observeEvent(input$test, {
          if(input$test){
          print("hello")
          }
          })
          }

          shinyApp(ui, server)





          share|improve this answer


























          • That works, but I’ve changed your code slightly in order to use the button as a trigger. So I’ve added priority: "event" option as it was described in the section 'Values vs. Events' of this Shiny manual page.

            – asvx
            Nov 18 '18 at 11:51











          • Could you clarify why did you use if(input$test)? The code works fine for me without it.

            – asvx
            Nov 18 '18 at 11:58











          • @asvx You're right, the if is useless. And you're right to use priority: "event". I didn't know priority: "event", I used a trick before to trigger the input change when the value does not change, but using priority: "event" is definitely better. Thanks.

            – Stéphane Laurent
            Nov 19 '18 at 9:34
















          1














          You have to set extend = "collection", like this:



          library(DT)
          datatable(iris,
          extensions = 'Buttons',
          options = list(
          dom = 'Bfrtip',
          buttons = list(
          "copy",
          list(
          extend = "collection",
          text = 'test',
          action = DT::JS("function ( e, dt, node, config ) {
          alert( 'Button activated' );
          }")
          )
          )
          )
          )


          But the action can only execute some Javascript, not a R command. However you can execute a R command by clicking a custom button if you put the datatable in a shiny application. Something like that:



          library(shiny)
          library(DT)

          ui <- basicPage(
          DTOutput("dtable")
          )

          server <- function(input, output, session){
          output$dtable <- renderDT(
          datatable(iris,
          extensions = 'Buttons',
          options = list(
          dom = 'Bfrtip',
          buttons = list(
          "copy",
          list(
          extend = "collection",
          text = 'test',
          action = DT::JS("function ( e, dt, node, config ) {
          Shiny.setInputValue('test', true);
          }")
          )
          )
          )
          )
          )

          observeEvent(input$test, {
          if(input$test){
          print("hello")
          }
          })
          }

          shinyApp(ui, server)





          share|improve this answer


























          • That works, but I’ve changed your code slightly in order to use the button as a trigger. So I’ve added priority: "event" option as it was described in the section 'Values vs. Events' of this Shiny manual page.

            – asvx
            Nov 18 '18 at 11:51











          • Could you clarify why did you use if(input$test)? The code works fine for me without it.

            – asvx
            Nov 18 '18 at 11:58











          • @asvx You're right, the if is useless. And you're right to use priority: "event". I didn't know priority: "event", I used a trick before to trigger the input change when the value does not change, but using priority: "event" is definitely better. Thanks.

            – Stéphane Laurent
            Nov 19 '18 at 9:34














          1












          1








          1







          You have to set extend = "collection", like this:



          library(DT)
          datatable(iris,
          extensions = 'Buttons',
          options = list(
          dom = 'Bfrtip',
          buttons = list(
          "copy",
          list(
          extend = "collection",
          text = 'test',
          action = DT::JS("function ( e, dt, node, config ) {
          alert( 'Button activated' );
          }")
          )
          )
          )
          )


          But the action can only execute some Javascript, not a R command. However you can execute a R command by clicking a custom button if you put the datatable in a shiny application. Something like that:



          library(shiny)
          library(DT)

          ui <- basicPage(
          DTOutput("dtable")
          )

          server <- function(input, output, session){
          output$dtable <- renderDT(
          datatable(iris,
          extensions = 'Buttons',
          options = list(
          dom = 'Bfrtip',
          buttons = list(
          "copy",
          list(
          extend = "collection",
          text = 'test',
          action = DT::JS("function ( e, dt, node, config ) {
          Shiny.setInputValue('test', true);
          }")
          )
          )
          )
          )
          )

          observeEvent(input$test, {
          if(input$test){
          print("hello")
          }
          })
          }

          shinyApp(ui, server)





          share|improve this answer















          You have to set extend = "collection", like this:



          library(DT)
          datatable(iris,
          extensions = 'Buttons',
          options = list(
          dom = 'Bfrtip',
          buttons = list(
          "copy",
          list(
          extend = "collection",
          text = 'test',
          action = DT::JS("function ( e, dt, node, config ) {
          alert( 'Button activated' );
          }")
          )
          )
          )
          )


          But the action can only execute some Javascript, not a R command. However you can execute a R command by clicking a custom button if you put the datatable in a shiny application. Something like that:



          library(shiny)
          library(DT)

          ui <- basicPage(
          DTOutput("dtable")
          )

          server <- function(input, output, session){
          output$dtable <- renderDT(
          datatable(iris,
          extensions = 'Buttons',
          options = list(
          dom = 'Bfrtip',
          buttons = list(
          "copy",
          list(
          extend = "collection",
          text = 'test',
          action = DT::JS("function ( e, dt, node, config ) {
          Shiny.setInputValue('test', true);
          }")
          )
          )
          )
          )
          )

          observeEvent(input$test, {
          if(input$test){
          print("hello")
          }
          })
          }

          shinyApp(ui, server)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 17 '18 at 18:59

























          answered Nov 17 '18 at 18:33









          Stéphane LaurentStéphane Laurent

          13.1k75393




          13.1k75393













          • That works, but I’ve changed your code slightly in order to use the button as a trigger. So I’ve added priority: "event" option as it was described in the section 'Values vs. Events' of this Shiny manual page.

            – asvx
            Nov 18 '18 at 11:51











          • Could you clarify why did you use if(input$test)? The code works fine for me without it.

            – asvx
            Nov 18 '18 at 11:58











          • @asvx You're right, the if is useless. And you're right to use priority: "event". I didn't know priority: "event", I used a trick before to trigger the input change when the value does not change, but using priority: "event" is definitely better. Thanks.

            – Stéphane Laurent
            Nov 19 '18 at 9:34



















          • That works, but I’ve changed your code slightly in order to use the button as a trigger. So I’ve added priority: "event" option as it was described in the section 'Values vs. Events' of this Shiny manual page.

            – asvx
            Nov 18 '18 at 11:51











          • Could you clarify why did you use if(input$test)? The code works fine for me without it.

            – asvx
            Nov 18 '18 at 11:58











          • @asvx You're right, the if is useless. And you're right to use priority: "event". I didn't know priority: "event", I used a trick before to trigger the input change when the value does not change, but using priority: "event" is definitely better. Thanks.

            – Stéphane Laurent
            Nov 19 '18 at 9:34

















          That works, but I’ve changed your code slightly in order to use the button as a trigger. So I’ve added priority: "event" option as it was described in the section 'Values vs. Events' of this Shiny manual page.

          – asvx
          Nov 18 '18 at 11:51





          That works, but I’ve changed your code slightly in order to use the button as a trigger. So I’ve added priority: "event" option as it was described in the section 'Values vs. Events' of this Shiny manual page.

          – asvx
          Nov 18 '18 at 11:51













          Could you clarify why did you use if(input$test)? The code works fine for me without it.

          – asvx
          Nov 18 '18 at 11:58





          Could you clarify why did you use if(input$test)? The code works fine for me without it.

          – asvx
          Nov 18 '18 at 11:58













          @asvx You're right, the if is useless. And you're right to use priority: "event". I didn't know priority: "event", I used a trick before to trigger the input change when the value does not change, but using priority: "event" is definitely better. Thanks.

          – Stéphane Laurent
          Nov 19 '18 at 9:34





          @asvx You're right, the if is useless. And you're right to use priority: "event". I didn't know priority: "event", I used a trick before to trigger the input change when the value does not change, but using priority: "event" is definitely better. Thanks.

          – Stéphane Laurent
          Nov 19 '18 at 9:34


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351684%2fis-there-any-way-to-add-custom-r-function-action-to-a-button-in-dt%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini