reactiveValues 不会在嵌套的 observeEvent 上更新

reactiveValues ​does not update on nested observeEvent

我试图制作一个最小的可重现代码来模拟我的问题。我想知道为什么 RESCID$rr 对象没有出现在 renderDT 中。当我注释掉命令 output$noshow 时,我可以看到 output$showw 的结果。先验的我还以为reactiveValues没有更新。但是,当添加 output$showw 并注释掉之前的输出时,我发现情况并非如此!感谢您的帮助。

ui <- fluidPage(

  # App title ----
  titlePanel("CRM"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Choose dataset ----
      actionButton("rockk", "Press rock!")

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      uiOutput("showw"),
      uiOutput("noshow")

    )

  )
)

server <- function(input, output) {

  # Reactive value for selected dataset ----
  datasetInput <- reactive({
    rock
  })

  RESCID <- reactiveValues(rr = NULL)
  
  observeEvent(input$rockk,{
   if(any(names(datasetInput())%in%'area') == TRUE){
    newvar  <- datasetInput()$area/datasetInput()$peri
    media <- mean(newvar)

    if(media > 3){
       
     showModal(modalDialog(
         title = "Important!",
         "Would you like to add a constant to the data to increase the mean?",
         footer = tagList(
                          actionButton("simfiltro","Yes"),
                          actionButton("naofiltro","No")
                          )
          ))
            
      observeEvent(input$simfiltro, {
             removeModal()
             
             newarea <- datasetInput()$area + 300 
             dados <- data.frame(datasetInput(),newarea)
             RESCID$rr <- dados 
             RESCID$rr
         })
  }else{
    RESCID$rr <- datasetInput()
    RESCID$rr
  } 
 }
})

output$noshow<- DT::renderDT({
                          req(RESCID$rr)
                          DT::datatable(
                           RESCID$rr,
                           escape=TRUE,
                           options = list(
                           pageLength = 10,
                           autoWidth = TRUE,
                           scrollX = TRUE)
                          )
                          })
output$showw <- renderUI({  
            HTML(paste('dataframe rows:', nrow(RESCID$rr)))    
    })
    
    

}

shinyApp(ui, server)

你有一些问题。嵌套反应不是一个好主意。我已经放在外面了。此外,您需要指定当用户在模式对话框中单击 No 时应该发生什么。最后,DTOutputrenderDT 一致,而不是 uiOutput。试试这个

library(DT)
ui <- fluidPage(
  
  # App title ----
  titlePanel("CRM"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Choose dataset ----
      actionButton("rockk", "Press rock!")
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      uiOutput("showw"),
      DTOutput("noshow")
    )
    
  )
)

server <- function(input, output) {
  
  # Reactive value for selected dataset ----
  datasetInput <- reactive({
    rock
  })
  
  RESCID <- reactiveValues(rr = NULL)
  
  observeEvent(input$rockk,{
    if(any(names(datasetInput())%in%'area') == TRUE){
      newvar  <- datasetInput()$area/datasetInput()$peri
      media <- mean(newvar)
      if(media > 3){
        
        showModal(modalDialog(
          title = "Important!",
          "Would you like to add a constant to the data to increase the mean?",
          footer = tagList(
            actionButton("simfiltro","Yes"),
            actionButton("naofiltro","No")
          )
        ))
        
        
      }else{
        RESCID$rr <- datasetInput()
        #RESCID$rr
      } 
    }
  })
  
  observeEvent(input$simfiltro, {
    removeModal()
    newvar  <- datasetInput()$area/datasetInput()$peri
    newarea <- datasetInput()$area + 300 
    dados <- data.frame(datasetInput(),newarea)
    RESCID$rr <- dados 
    #RESCID$rr
  })
  
  observeEvent(input$naofiltro, {
    removeModal()
    RESCID$rr <- datasetInput()
  })
  
  output$noshow<- renderDT({
    req(RESCID$rr)
    DT::datatable(
      RESCID$rr,
      escape=TRUE,
      options = list(
        pageLength = 10,
        autoWidth = TRUE,
        scrollX = TRUE)
    )
  })
  output$showw <- renderUI({  
    HTML(paste('dataframe rows:', nrow(RESCID$rr)))    
  })
  
}

shinyApp(ui, server)