Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updateTree() not working in shiny modules #84

Open
fungs opened this issue Aug 14, 2019 · 8 comments
Open

updateTree() not working in shiny modules #84

fungs opened this issue Aug 14, 2019 · 8 comments

Comments

@fungs
Copy link

fungs commented Aug 14, 2019

updateTree() is recommended to be used when node selection does not trigger observeEvent(input$tree, {}) after the output is changed via renderTree.

Unfortunately, at least for me, the same tree that draws well with renderTree() does not work with updateTree() because the displayed tree remains empty. This happens if run in a Shiny module.

My suspicion is that it is caused by the fact, that session is a different kind of object in a shiny module or because the given tree identifier does not match the one defined in the UI using the names namespace wrapper function NS("id")("tree_id").

I'm sorry I cannot be more verbose with an example but it should be straightforward to reproduce.

@trafficonese
Copy link
Contributor

Can you include a small example of that issue?

Maybe it is related to #81 and I just opened a PR (#85) which should fix it. Maybe it fixes your issue aswell?

@fungs
Copy link
Author

fungs commented Aug 23, 2019

Please leave this issue open, I will try to come with more details once I'm back to implementing this functionality in code. Thanks.

@trafficonese
Copy link
Contributor

Is this still an issue?

@psimm
Copy link

psimm commented Jun 4, 2020

I still see the issue with the current version of shinyTree from Master branch. Here is an example of an app with a shinyTree and an actionButton that should select all nodes. updateTree works without the module but doesn't do anything when used within the module.

Please correct me if it is an error on my part using the module.

With module (doesn't work)

library(shiny)
library(shinyTree)

treeModuleUI <- function(id) {
  ns <- NS(id)
  tagList(
    shinyTree(outputId = ns("tree"), checkbox = TRUE),
    actionButton(inputId = ns("select"), label = "Select all")
  )
}

treeModule <- function(input, output, session) {
  ns <- session$ns
  
  data <- list(root1 = "", root2 = "", root3 = "")
  output$tree <- renderTree(data)
  
  observeEvent(input$select, {
    message("Clicked on select all")
    for (i in seq_along(data)) {
      attr(data[[i]], "stselected") <- TRUE
    }
    updateTree(session, treeId = ns("tree"), data = data)
  })
} 

ui <- fluidPage(
  treeModuleUI(id = "tree1")
)

server <- function(input, output, session) {
  callModule(treeModule, id = "tree1")
}

shinyApp(ui, server)

Without module (works)

library(shiny)
library(shinyTree)

ui <- fluidPage(
  shinyTree(outputId = "tree", checkbox = TRUE),
  actionButton(inputId = "select", label = "Select all")
)

server <- function(input, output, session) {
  data <- list(root1 = "", root2 = "", root3 = "")
  output$tree <- renderTree(data)
  
  observeEvent(input$select, {
    message("Clicked on select all")
    
    for (i in seq_along(data)) {
      attr(data[[i]], "stselected") <- TRUE
    }
    updateTree(session, treeId = "tree", data = data)
  })
}

shinyApp(ui, server)

@trafficonese
Copy link
Contributor

Try without ns for the updateTree:
updateTree(session, treeId = "tree", data = data)
That worked for me.

@psimm
Copy link

psimm commented Jun 4, 2020

Yes, that works. Thank you!

@fungs
Copy link
Author

fungs commented Jun 7, 2020

Just to understand: how does updateTree() know the correct shiny context? It seems it does infer the correct namespace somehow.

@trafficonese
Copy link
Contributor

From my understanding, this is just normal shiny module magic, updateTree is called inside the server-part of the module and ns() normally should just be called within the UI functions.
You can also see that observeEvent is triggered by input$select without the need of adding the module-namespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants