Tinniam V. Ganesh
Tinniam V. Ganesh

Reputation: 2079

Shiny selectInput flips to the top after display

My Shiny app has 2 input a) A drop down list b) A list with a scroll bar. I find that when I select a value by scrolling down, after the command is executed the scroll window flips back to the top of the 20 of the list and does not stay on the selected value.

This is the code in server.R

 # Analyze and display bowler's plots
    output$bowlerPlot <- renderPlot({  
    load("./bowler.RData")
        IPLBowlers <-b        
        # Render dynamic UI
        output$bowlerFunctionList = renderUI({
        selectInput('bowlerFunc', 'Choose function',choices=IPLBowlerFuncs,selected=input$bowlerFunc)
        })
        output$bowlerList = renderUI({
        selectInput('bowler', 'Choose chart type',choices=IPLBowlers,selected=input$bowler,
         selectize=FALSE, size=20)
        })

        analyzeIPLBowlers(input$bowler,input$bowlerFunc)

    })

and ui.R

 # Application title
      titlePanel("Analyze bowlers"),                            
      fluidRow(
      column(3,
                uiOutput("bowlerFunctionList"), 
                uiOutput("bowlerList")                                       
               ),                                
               # Show a plot of the generated distribution        
               column(6,
                      plotOutput("bowlerPlot")
                ),

            )
         )    

Please let me know this can be fixed.

Upvotes: 1

Views: 592

Answers (1)

Enzo
Enzo

Reputation: 2611

The structure of our code is not right.

While a renderUI function can include one (or many) renderPlot function, the reverse is usually not true (and in your case leads to unpredictable results!).

I am amazed that you get anything displayed at all. Make sure that renderPlot does not include the two renderUI functions and it should work.

If it is just the matter to update the selectInput server side, you can use updateSelectInput.

Just as a possible example of updateSelectInput use, I've lifted from Function reference version 0.14.2 the following code:

library(shiny)
ui <- fluidPage(
  p("The checkbox group controls the select input"),
  checkboxGroupInput("inCheckboxGroup", "Input checkbox",
                     c("Item A", "Item B", "Item C")),
  selectInput("inSelect", "Select input",
              c("Item A", "Item B", "Item C"))
)
server <- function(input, output, session) {
  observe({
    x <- input$inCheckboxGroup
    # Can use character(0) to remove all choices
    if (is.null(x))
      x <- character(0)  
    # Can also set the label and select items
    updateSelectInput(session, "inSelect",
                      label = paste("Select input label", length(x)),
                      choices = x,
                      selected = tail(x, 1)
    )
  })
}
shinyApp(ui, server)

Upvotes: 2

Related Questions