Comments (2)
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.
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)
- With Bootstrap 5, `uiOutput()` doesn't dim in opacity when recalculating
- Plot overlaps onto tab panel. HOT 2
- shiny > 1.8.0 breaks tags$button reactivity HOT 6
- Set outputOtions at assignment
- Long running Shiny app starts printing lots of "nothing to compact" messages HOT 2
- Allow `OnUnhandledError` to be scoped to modules
- Warning: Error in ggplot2::geom_boxplot: Problem while converting geom to grob.
- Nested future-promises lose reactive context
- Problem with chromote package HOT 1
- Updating reactive value on a timer results on JS error
- The issue of setting updateSelectInput's selected parameter to null HOT 3
- Parent container created by `shiny::tabPanel()` not taking full vertical container space
- Shiny Getting Started Tutorial missing `textOutput('selectedVar')` from example HOT 2
- Feature request: Add option to change radioButtons appearance similar to actionButtons
- R Shiny does not open a model training in Python HOT 1
- App with no outputs never stops pulsing
- shiny app examples crashes without error HOT 6
- observeEvent does not trigger to changes in reactiveValues HOT 4
- Can't add another ExtendedTask while the one is running? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from shiny.