Home > front end >  reactiveValues ​does not update on nested observeEvent
reactiveValues ​does not update on nested observeEvent

Time:05-11

I tried to make a minimal reproducible code that tries to simulate my problem. I would like to know why the RESCID$rr object does not appear in renderDT. When I comment out the command output$noshow I can see the result of output$showw. A priori I thought that reactiveValues ​​was not updating. However, when adding the output$showw and commenting out the previous output, I saw that it is not quite that!Thanks for any help.

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)

CodePudding user response:

You have a few issues. Nesting reactives is not a good idea. I have put it outside. Also, you need to specify what should happen when user clicks on No in the modal dialog. Lastly, DTOutput goes with renderDT, and not uiOutput. Try this

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)
  • Related