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

unclear and limited naming of dynamic modules #4

Open
srearl opened this issue Apr 26, 2023 · 1 comment
Open

unclear and limited naming of dynamic modules #4

srearl opened this issue Apr 26, 2023 · 1 comment

Comments

@srearl
Copy link
Contributor

srearl commented Apr 26, 2023

The code block below facilitates populating annual cover events with observation (cover) data. While this works as intended such that clicking on a button to populate a cover event calls another module that facilitates adding the actual cover data; clicking on another cover event removes the module associated with the previous event and raises a new module of cover data associated with the newly selected cover event. There are two problems. First, the cover event id to populate is always cover_event_0 in the DOM. This is a problem because it is not clear why. More importantly, it makes it unclear how to remove the cover composition module associated with the cover event from another part of the application. Here is an example: click to populate a particular cover event, then click to edit a different cover event. In that scenario, the module to populate the first cover even remains visible even though the user elected to edit the details of a different cover event. This is not necessarily a serious concern or impediment to the workflow but best would be to have the module to populate the cover event removed when any action on another cover event is selected just to avoid potential confusion.

    populate_counter <- shiny::reactiveVal(value = 0)

    shiny::observeEvent(input$cover_event_to_populate, {

      id            <- populate_counter()
      ce_element_id <- paste0("ce_element_", id)

      message("from populate: ", ce_element_id)

      shiny::insertUI(
        selector = "#add_cover_compositions",
        where    = "afterBegin",
        ui       = tags$div(
          id = ce_element_id,
          cover_compositionUI(ns(paste0("cover_composition_inventory", id)))
        )
      )

      cover_composition(
        id             = paste0("cover_composition_inventory", id),
        ce_to_populate = this_ce_to_populate
      )

      
      populate_counter(populate_counter() + 1) # increment module counter

      if (populate_counter() > 1) {

        shiny::removeUI(selector = paste0("#", ce_element_id))
        remove_shiny_inputs(ns(paste0("cover_composition_inventory", id)), input)
        remove_shiny_inputs(ns(paste0("cover_composition_inventory", id, "-cover_composition_inventory", id)), input)

      }

    },
    once       = FALSE,
    ignoreInit = TRUE
    )
@srearl
Copy link
Contributor Author

srearl commented Oct 27, 2023

see the arthropods-shiny application for more on this, especially {notes} in this module

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

1 participant