thothal
thothal

Reputation: 20399

input$shinyTree does not get updated after new render

In the shiny app below, I have a reset button, which, well, resets the shinyTree. I figured that the corresponding input$tree does not get updated after pressing the reset, i.e. input$tree still reflects the un-reset state. Try the app by moving some nodes and press the reset button and you will see that while the output gets reset the verbatimTextOutput still shows the old version.

Is that behavior intentional? How do I force shinyTree to update the input$tree whenever output$tree is changed? Would #89 solve my issue?

library(shiny)
library(shinyTree)

ui <- fluidPage(actionButton("reset", "reset"),
                shinyTree("tree", dragAndDrop=TRUE, sort = F, wholerow = T, unique = T),
                verbatimTextOutput("str"))



server <- function(input, output, session) {
  output$tree <- renderTree({
    input$reset
    list(
      root3 = "234",
      root1 = list(
        SubListA = list(leaf1 = "", leaf2 = "")
      ),
      root2 = list(
        SubListA = list(leaf1 = "", leaf2 = "")
      )
    )
  })

  output$str <- renderPrint({
    # shinyTrees will also be available as inputs so you can
    # monitor changes that occur to the tree as the user interacts
    # with it.
    str(input$tree)
  })
}

shinyApp(ui, server)

To clarify: I have read R Shiny - Resetting shinyTree node selections before posting this question and though related, it is not solving my problem, because I am not using updateTree in the first place and in my real use case things are way more complicated. Thus, the highlighted solution to use an additional reactiveVal won't work smoothly.

I really need a solution which assures that input$tree is up to date regardless whether the tree was updated through user interaction or through R code itself.

I have only a superficial understanding of shiny input widgets, but I guess that #89 could solve my issue as it will pass a new tree to shiny whenever there is a refresh. I will try this one out.

Upvotes: 2

Views: 560

Answers (3)

Eric Buehler
Eric Buehler

Reputation: 1

I would not recommend this solution in R 4.0.2:

devtools::install_github("shinyTree/shinyTree#89")
options(shinyTree.refresh = TRUE)

For me it caused a issue similar to this post, which I was only able to resolve after re-installing R.

Upvotes: 0

thothal
thothal

Reputation: 20399

Just for the reference and as already assumed, #89 solves this issue.

devtools::install_github("shinyTree/shinyTree#89")
options(shinyTree.refresh = TRUE)

Reason behind is that the shinyTree author decided not to callback on refresh (or setState for all it matters) thus input$tree does not "know" that it was changed and returns the stale tree. The new PR allows to explicitly callback on refresh (by setting the option), which solves my issue.

Upvotes: 0

ismirsehregal
ismirsehregal

Reputation: 33520

Please check the following, you can force to reset input$tree via JS:

library(shiny)
library(shinyTree)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  actionButton("reset", "reset"),
                shinyTree("tree", dragAndDrop=TRUE, sort = F, wholerow = T, unique = T),
                verbatimTextOutput("str"))

server <- function(input, output, session) {

  observeEvent(input$reset, {
    runjs("Shiny.setInputValue('tree', null);")
  })

  output$tree <- renderTree({
    input$reset
    list(
      root3 = "234",
      root1 = list(
        SubListA = list(leaf1 = "", leaf2 = "")
      ),
      root2 = list(
        SubListA = list(leaf1 = "", leaf2 = "")
      )
    )
  })

  output$str <- renderPrint({
    str(input$tree)
  })
}

shinyApp(ui, server)

Upvotes: 1

Related Questions