简体   繁体   中英

Why shiny can not detect the `data.tree` manipulations outside of an `observe`?

Does Shiny can detect only common R's objects? If yes, What objects can it observe?
For example, I tried many options with no success to detect a data.tree changes in shiny.
Does anyone know why this happens?

library(shiny)
library(data.tree)
data(acme)

ui <- fluidPage(
actionButton("go", "go" ),
tags$h2("text"),
verbatimTextOutput("text"),
tags$h2("text0"),
verbatimTextOutput("text0"),
tags$h2("text1"),
verbatimTextOutput("text1"),
tags$h2("text2"),
verbatimTextOutput("text2"),
tags$h2("text3"),
verbatimTextOutput("text3"),
tags$h2("text4"),
verbatimTextOutput("text4")
)

server <- function(input, output, session) {
anum <- reactiveValues(a = 0)
a <- reactiveValues(acme = acme, f = NULL)
b <- reactiveVal(acme)
cc <- reactive(a$acme)
observeEvent(input$go, {
  z = sample(x = 1:100 , size = 1)
  a$cach <<- a$acme$clone()
  anum$a <<- anum$a + 1
  a$acme$AddChild(paste0("New", z))
  a$f <<-  a$acme
  b(a$acme)
  print("a$acme")
  print(a$acme)
  print("b()")
  print(b())
})

### not working
output$text = renderPrint( print(a$f) )
output$text0 = renderPrint(print(b()))
output$text1 = renderPrint(print(cc()))

### working
observe({
  print(identical(a$acme, a$cach))
  output$text2 = renderPrint(print(b()))
})
### working
observe({
  anum$a
  output$text3 = renderPrint(print(a$acme))
})
### working
observeEvent(eventExpr =  anum$a, handlerExpr = {
  output$text4 = renderPrint(print(a$acme))
})
}

shinyApp(ui, server)

Turns out that adding:

        a$f <- 0 #to force reaction
        a$f <-  a$acme
        a$acme <- 0 #to force reaction
        a$acme <- a$f

'fixed' the problem.

library(shiny)
library(data.tree)
data(acme)

ui <- fluidPage(
    actionButton("go", "go" ),
    verbatimTextOutput("text"),
    verbatimTextOutput("text1"),
    verbatimTextOutput("text2"),
    verbatimTextOutput("text3"),
    verbatimTextOutput("text4")
    
)

server <- function(input, output, session) {
    
    anum <- reactiveValues(a = 0)
    a    <- reactiveValues(acme = acme, f = NULL)
    b    <- reactiveVal(acme)
    cc   <- reactive(a$acme)
    
    
    observeEvent(input$go, {
        z      <-  sample(x = 1:100 , size = 1)
        a$cach <- a$acme$clone()
        anum$a <- anum$a + 1
        a$acme$AddChild(paste0("New", z))
        a$f    <- 0 #to force reaction
        a$f    <-  a$acme
        a$acme <- 0 #to force reaction
        a$acme <- a$f
        b(a$acme)
        print("a$acme")
        print(a$acme)
        print("b()")
        print(b())
        
        
    })
    
    ### not working
    output$text = renderPrint({
        req(a$f)
        print(a$f)})
    
    output$text2 = renderPrint(print(cc()))
    
    ## now it works
    observe({
        print(identical(a$acme, a$cach)) #this is triggering the update

        output$text1 = renderPrint(print(b()))


    })
    ### working
    observe({
        anum$a
        output$text3 = renderPrint(print(a$acme))
    })
    ### working
    observeEvent(eventExpr =  anum$a, handlerExpr = {
        output$text4 = renderPrint(print(a$acme))
        
    })
    
    
}

shinyApp(ui, server)

I think there's something going on with a$acme$AddChild(paste0("New", z)) method that is not detected as a change when called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM