Git Product home page Git Product logo

Comments (2)

SokolovAnatoliy avatar SokolovAnatoliy commented on May 30, 2024 1

Thank you for a very clear explanation and links @scal-o! As that blog pointed out, I have been pulling my hair out about it.

from shiny.

scal-o avatar scal-o commented on May 30, 2024

Hi! A little clarification: what is causing the problem here are not the duplicated "server objects", but rather the observer objects that they create. Every time you call selectInputServer(*id*) you create a new observer object with a binding to the *id*-show_popup input. So every time you increase the numeric input, you create a new series of observers all bound to the respective *id*-show_popup inputs, and that's why the shinyalert gets triggered multiple times.

One thing you could do to avoid issues (like creating the wrong number of objects and then just having them lying around occupying space and possibly resources) is destroying all of the previously created observers and creating anew only the ones you actually need every time input$num changes. This can be done via the destroy() method of the observer objects. Here is an example of how you could do it.

In the module server, list all of the observers you create to the session$userData$choose_a_name list. This will make the observer objects visible from the main server function.

selectInputServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    print(paste("server", id))
    
    session$userData$observers[[id]] <- observe({
      shinyalert::shinyalert(title = "Success!", text = {
        "It worked!!"
      }, type = "success")
      
    }) %>% bindEvent(input$show_popup, ignoreInit = TRUE)
    
  })
}

In the main server function, call the destroy method on every observer object listed in session$userData$choose_a_name, and then generate the observers you need with lapply.

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

    if (length(session$userData$observers) != 0) {
      session$userData$observers <- lapply(
        session$userData$observers,
        function(obs) obs$destroy())
    }

    lapply(1:input$num, function(i) {
      selectInputServer(paste0("select", i))
    })

  })
  
  output$selectInputs <- renderUI({
    lapply(1:input$num, function(i) {
      selectInputUI(paste0("select", i))
    })
  })
  
  
}

By using this method you would also not need the numModules counter.
You can read some documentation about observer methods at ?observe, in the value section.
You can read about the session$userData environment at ?session, in the value section.
For a perhaps more clear explanation of what the code above does, look into this.

from shiny.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.