juba / shinyglide

Glide.js component for Shiny apps
https://juba.github.io/shinyglide/
91 stars 8 forks source link

Why the next_condition parameter in screenOutput() doesn't work the first time the screen is rendered? #36

Closed JulianoAtto closed 1 year ago

JulianoAtto commented 1 year ago

Hi, thanks for this package. It's amazing!

Questions

  1. Why when I click the "Next" button and then return to the first screen using the "Back" button, the condition works and I can't go forward until I click in at least one option, but the first time it does not work that way?

Explanation

I use insertUI() with a glide() inside it in the server function. To create the first screen of the glide I use screenOutput() and since shinyglide use the same syntax as conditionalPanel() the next_condition parameter is based in this conditionalPanel() example.

Code

# libraries
library(shiny)
library(shinyglide)
library(shinyjs)

# ui
ui <- fluidPage(useShinyjs(),
                tags$div(id = 'placeholder'),
                actionButton("btn",
                             "button"))

# server
server <- function(input, output, session) {
    # insertUI() when btn is clicked
    observeEvent(input$btn, {
        disable("btn")
        insertUI(selector = '#placeholder',
                 ui = tags$div(fixedPage(
                     glide(
                         id = "glide",
                         screenOutput(outputId = "screen",
                                      next_condition = "output['next_condition'] == 1"),
                         screen(p("Second screen."))
                     )
                 )))
    })

    # screenOutput() for the first screen
    output$screen <- renderUI({
        checkboxGroupInput("checkbox", "checkbox", list("A", "B"))
    })
    # condition to shinyglide works
    outputOptions(output, "screen", suspendWhenHidden = FALSE)

    # output variable in the server code that is used in argument 'next_condition' in screenOutput()
    output$next_condition <- reactive({
        if (isTruthy(input$checkbox)) {
            1
        } else {
            0
        }

    })
    # condition to output works
    outputOptions(output, "next_condition", suspendWhenHidden = FALSE)

}

shinyApp(ui, server)
juba commented 1 year ago

Hi,

I think this should now be fixed in the development version. Let me know if it is not the case.

Thanks for taking the time to report the issue.