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

Server rendered ui elements render incorrectly when used twice #135

Open
Mikea0228 opened this issue Sep 3, 2021 · 0 comments
Open

Server rendered ui elements render incorrectly when used twice #135

Mikea0228 opened this issue Sep 3, 2021 · 0 comments

Comments

@Mikea0228
Copy link

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)
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