Skip to content

dash callback_context in R? #208

Closed
Closed

Description

@FerusAndBeyond

In python dash.callback_context can be used to see what triggers the callback, how can this be done in dashR?

Activity

  1. rpkyle commented on Jun 3, 2020

    @rpkyle
    Contributor

    Hi @FerusAndBeyond! Thanks for your interest in Dash for R. callback_context is indeed available (since #87).

    All that's required is to access app$callback_context() from within a callback -- if you try to call this method from outside a callback, Dash for R will present a helpful reminder.

    Here's a demo, apologies that it isn't very interesting, but it will give you the general idea:

    library(dash)
    library(dashHtmlComponents)
    
    app <- Dash$new()
    
    app$layout(
      htmlDiv(list(
        htmlButton('Button 1', id='btn-1'),
        htmlButton('Button 2', id='btn-2'),    
        htmlButton('Button 3', id='btn-3'),
        htmlDiv(id='container')
      )
      )
    )
    
    app$callback(output=list(id='container', property='children'),
                 params=list(
                   input(id='btn-1', property='n_clicks'),
                   input(id='btn-2', property='n_clicks'),
                   input(id='btn-3', property='n_clicks')),
                 function(btn1, btn2, btn3)
                 {
                   ctx <- app$callback_context()
                   
                   if (is.null(ctx$triggered[["value"]][[1]])) {
                     msg <- 'None of the buttons have been clicked yet'
                   } else {
                     button_num <- ctx$triggered[["prop_id"]]
                     msg <- sprintf('Button %s was most recently clicked', unlist(strsplit(button_num, split = "[.]"))[1])
                   }
                   
                   ctx_msg <- jsonlite::prettify(dash:::to_JSON(ctx))
                   
                   return(htmlDiv(list(
                     htmlDiv(sprintf('btn1 : %s', btn1)),
                     htmlDiv(sprintf('btn2 : %s', btn2)),
                     htmlDiv(sprintf('btn3 : %s', btn3)),
                     htmlDiv(msg),
                     htmlPre(ctx_msg)
                   )))
                 }
    )
    
    app$run_server()

    Let me know if this helps or you have other questions -- if this answered your question, I'll go ahead and close the issue.

  2. rpkyle commented on Jun 3, 2020

    @rpkyle
    Contributor

    @FerusAndBeyond There's also a bit about this feature in our online documentation for R, in case it's helpful:

    https://dashr.plotly.com/advanced-callbacks

  3. FerusAndBeyond commented on Jun 3, 2020

    @FerusAndBeyond
    Author

    @rpkyle Thanks! Must have missed that page and nothing came up when googling. I'll close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions