0

My code is not working as intended. Let me describe a scenario that demonstrates the issue.

  • A user selects "Action 1" on the 1st tab. Then he/she selects "Choice 1" on the 2nd tab. "Select a node" and "Select a variable" are shown. ---> THIS IS INTENDED.

  • Same user goes back to the 1st tab and selects "Action 2." When he/she goes to the 2nd tab, he/she selects "Choice 3." Now, "Select a node" and "Select a variable" are still shown. --- > THIS IS NOT CORRECT. They should only appear if a user selects "Choice 1" or "Choice 2."

So, my Shiny app seems to remember the previous selection. Any idea how I can update my codes so it works as intended? Thanks!

server.R:

function(input, output, session) {

    # Update SelectInput

    observe({
      if (input$action=="1") {
          updateSelectInput(session, "selectnode", choices = "Total")
      } else {
          updateSelectInput(session, "selectnode", choices = c('Node 1', 'Node 2', 'Node 3'))
      }
    })

    # Update SelectInput for selectvar

    observe({
        updateSelectInput(session, "selectvar", choices = c('A','B','C'))
    })

    observeEvent(input$act_next, {
      updateTabsetPanel(session, "allResults", 'structure')
    })
}

ui.R:

fluidPage(theme = shinytheme("spacelab"),

    navbarPage("Structure", id = "allResults",

        tabPanel(value='action', title='User Actions', 
            verticalLayout(
                    h4("Select an action"),
                    radioButtons("action", label = "",
                                  choices  = list("Action 1"  = 1, 
                                                  "Action 2" = 2), 
                                  selected = 1),
                    br(),
                    actionButton("act_next", "Next!")
            )
        ),

        tabPanel(value='structure', title='Main',
            verticalLayout(
                    conditionalPanel(
                      condition = ("input.action==1"), 
                      selectInput("dd_options", label = "Select one option", 
                                   choices = c(
                                    "Choice 1"              = 1, 
                                    "Choice 2"              = 2), 
                                   selected = 1)

                    ),

                    conditionalPanel(
                      condition = ("input.action==2"), 
                      selectInput("dd_options", label = "Select one option", 
                                   choices = c(
                                    "Choice 1"             = 1,
                                    "Choice 2"             = 2, 
                                    "Choice 3"             = 3), 
                                    selected = 3)
                    ),

                    br(),

                    conditionalPanel(
                        condition = ("input.dd_options == 1 || input.dd_options == 2"), 
                        selectInput("selectnode", label = "Select a node", choices = ""),
                        br()
                    ),

                    conditionalPanel(
                        condition = ("input.dd_options == 1 || input.dd_options == 2" ),
                        selectInput("selectvar", label = "Select a variable", choices = ""),
                        br()
                    )
                )        
)))
Ketty
  • 811
  • 10
  • 21

1 Answers1

0

I think your issue is due to repeating dd_options. Per this answer, input identifiers (and output identifiers) cannot be repeated.

Instead, I would use a reactive UI that changes based upon your input$action. I've also consolidated your conditionalPanel.

Try this out:

library(shiny)
library(shinythemes)

ui <- fluidPage(theme = shinytheme("spacelab"),
                navbarPage("Structure", id = "allResults",
                           tabPanel(value='action', title='User Actions', 
                                    verticalLayout(
                                        h4("Select an action"),
                                        radioButtons("action", label = "",
                                                     choices  = list("Action 1"  = 1, 
                                                                     "Action 2" = 2), 
                                                     selected = 1),
                                        br(),
                                        actionButton("act_next", "Next!")
                                    )
                           ),
                           tabPanel(value='structure', title='Main',
                                    verticalLayout(
                                        uiOutput("dd_options"),

                                        conditionalPanel(
                                            condition = ("input.dd_options == 1 || input.dd_options == 2"), 
                                            selectInput("selectnode", label = "Select a node", choices = ""),
                                            br(),
                                            selectInput("selectvar", label = "Select a variable", choices = "")
                                        ) 
                           ))))

server <- function(input, output, session) {

    # Update SelectInput
    observe({
        if (input$action==1) {
            updateSelectInput(session, "selectnode", choices = "Total")
        } else {
            updateSelectInput(session, "selectnode", choices = c('Node 1', 'Node 2', 'Node 3'))
        }
    })

    # Update SelectInput for selectvar
    observe({
        updateSelectInput(session, "selectvar", choices = c('A','B','C'))
    })
     observeEvent(input$act_next, {
         updateTabsetPanel(session, "allResults", 'structure')
     })

    # reactive UI for dd_options choices 
    output$dd_options <- renderUI({
        if(input$action == 1) {
            choices <- c("Choice 1" = 1,
                         "Choice 2" = 2)
            selected <- 1
        } else if(input$action == 2) {
            choices <- c("Choice 1" = 1,
                         "Choice 2" = 2,
                         "Choice 3" = 3)
            selected <- 3
        }
        selectInput("dd_options", label = "Select one option", 
                    choices = choices, 
                    selected = selected)
    })
}

shinyApp(ui, server)
Hallie Swan
  • 2,714
  • 1
  • 15
  • 23