Skip to content

Server rendered ui elements render incorrectly when used twice #135

@Mikea0228

Description

@Mikea0228

If a material_dropdown() is used within a server side call of renderUI() when there is an additional material_dropdown() defined in the UI: the server side dropdown will not render correctly. A workaround to allow for multiple dropdowns to be defined by server side logic is to call update_material_dropdown() within a non reactive part of server .

Below are some examples of when multiple dropdowns so not render properly and an example of the work around using update_material_dropdown()

library(shiny)
library(shinymaterial)

#two dropdowns defined manually works fine
ui_0 <- material_page(
    material_dropdown("dropdown1",label = "Dropdown 1",choices = letters[1:3]),
    material_dropdown("dropdown2",label = "Dropdown 2",choices = letters[4:6]),
    "More content"
)

#define a ui with a server defined dropdown, this works as intended
ui_1 <- material_page(
    uiOutput("server_dropdown"),
    "More content",

)
#a ui with a second dropdown, this does not work
ui_2 <- material_page(
    uiOutput("server_dropdown"),
    "More content",
    material_dropdown(
        input_id = "dropdown2",
        label = "dropdown 2",
        choices = letters[1:5]
    )
)

#define a ui with a server defined dropdown and another dropdown in another part 
#the ui, not working as intended
ui_3 <- material_page(
    uiOutput("server_dropdown"),
    "More content",
    material_modal(
        modal_id = "modal",
        button_text = "pressme",
        title = "my modal",
        material_dropdown(
            input_id = "dropdown2",
            label = "dropdown 2 in a modal",
            choices = letters[1:5]
        )
    )
)    
server_0 <- function(input, output, session) {
    #define choices of a dropdwon using R
    server_choices <- sample(letters, 3)
    
    output$server_dropdown <- renderUI({
        material_dropdown("dropdown1",
                          label = "dropdown 1 outside a modal",
                          choices = server_choices)
    })
}

server_1 <- function(input,output,session){
    server_choices <- sample(letters, 3)
    update_material_dropdown(session,input_id = "dropdown1",choices = server_choices,value=server_choices[1])
    
}

#works as intended, two static dropdowns
shinyApp(ui = ui_0,server = server_0)

#works as intended with one server side dropdown
shinyApp(ui = ui_1, server = server_0)
# does not work as intended, additional dropdown breaks first
shinyApp(ui = ui_2, server = server_0)
# even when not adjacent, format is broken
shinyApp(ui = ui_3,server = server_0)

# use workaround to define choices programatically
shinyApp(ui=ui_0,server = server_1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions