Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insertUI: Modal Close/Open resets inserted UI using insertUI & lapply #1590

Closed
bogdanrau opened this issue Feb 18, 2017 · 5 comments
Closed

Comments

@bogdanrau
Copy link

bogdanrau commented Feb 18, 2017

Possibly related to #1588.

I use insertUI and removeUI to insert dynamic filters in a modalDialog, and each filter has it's own #remove to remove that particular filter. When the modal closes to execute the query, and then is reopened, the filters disappear. The code below shows how these are added and removed. The intent of this is, once a user closes the modal (with an actionButton that executes the query), and they want to go back and modify, the inserted UI elements should be retained.

I tried keeping the input$addFilter number of clicks in a reactive value and triggering the observeEvent based on that, but it doesn't seem to work. Is this a limitation in the current implementation of modalDialog or a limitation in my understanding?

# Function to insert filters using insertUI
observeEvent(input$addFilter, {
  if (input$addFilter == 0) {
    
  } else {
    insertUI(
      selector = "#addFilter",
      where = "afterEnd",
      ui = fluidRow(id = paste0("newFilter", input$addFilter),
                    column(width = 4,
                           selectInput(inputId = paste0("addFilter-variable", input$addFilter), label = "Variable:", choices = paste0("Variable", input$addFilter), width = "100%")),
                    column(width = 3,
                           selectInput(inputId = paste0("addFilter-operator", input$addFilter), label = "Operator:", choices = c("EQUAL TO / IN", "NOT EQUAL TO"))),
                    column(width = 4,
                           selectInput(inputId = paste0("addFilter-level", input$addFilter), label = "Value:", choices = paste0("Value", input$addFilter), width = "100%")),
                    column(width = 1,
                           actionButton(inputId = paste0("removeFilter", input$addFilter), label = NULL, icon = icon("trash")))
      ))
  }

})


# Function to remove filters using removeUI
lapply(
  X = 1:30,
  FUN = function(i) {
    observeEvent(input[[paste0("removeFilter", i)]], {
      removeUI(
        selector = paste0("#newFilter", i)
      )
    })
  }
)
@bborgesr
Copy link
Contributor

This is definitely related to #1588. And the reason your workaround is not working is because saving the number of clicks isn't what's important in your scenario (like it was in #1588). What is important here is retaining the things you actually add to the UI when you click the button (in your example above, this would be the whole fluidRow with its 4 inputs: 3 selectInputs and the action button). The way to accomplish this NOW would be painful -- you'd need to save all of those in a reactiveValuesvariable and actually re-insert them every time since you'd still lose everything once you reopen the modal... So not only would your code look ugly, but you may also experience performance issues...

With the fix in #1589, this would just work as is (I think). Feel free to try if you want and tell me your thoughts. The problem is that we're not sure yet if that will make it into the master branch or not... As of now, modals simply do not support "persistent use" -- they're kind of meant to be opened once. Your situation is just so unfortunate though... This will might make us reconsider...

Anyway, in the meantime, if you can get a small minimal reproducible example, I could try to give you a rough solution that works now...

@bogdanrau
Copy link
Author

Thanks Barbara. That's actually how I handle some of the other inputs on that modal (storing last selections as reactives). I agree that saving the entire fluidRow with all the inputs would be extremely painful. I'll give the fix in #1589 a try and let you know my findings. Any concerns about the travis-ci failure?

Would bypassing the modalDialog completely and using HTML() with bootstrap tags be another alternative?

@bborgesr
Copy link
Contributor

Don't worry about the Travis failures. That code wouldn't be ready to merge to master (needs documentation, for example), but it's safe for you to try.

And yes, if you bypass Shiny modals completely, you shouldn't have this problem. I'm not sure how shinyBS handles modals, but that could be a possibility...

@bborgesr
Copy link
Contributor

Closing for now. See #1589 (comment) for details.

@jcheng5
Copy link
Member

jcheng5 commented Mar 21, 2017

The shinyBS modal functions should be perfect for you @bogdanrau.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants