How can I prevent R Shiny File Browser from reseting browser to top of the page?

Viewed 128

I'm creating an application in R Shiny where a user selects from a number of Sessions, and based on that input a condition panel displays the relevant number of fileInput widgets so that users can upload their files.

This works fine as long as the number of fileInputs does not exceed the length of the page (in my case, 5). However, if a user has to scroll down the page to get to the "Browse..." button, and then clicks on it, it causes the browser to go back to the top of the page.

The code still works and the file is still uploaded, but this becomes irritating and confusing if there are, say, 15 - 20 files to be uploaded and the user has to scroll back down each time.

Tl;dr hitting 'Browse...' on a FileInput widget causes the browser to go back to the top of the Shiny app.

Very simplistic working example below without any server code:

library(shiny)

ui <- fluidPage(
    
    sidebarLayout(
        sidebarPanel(
            selectInput(
                inputId = "numSessions",
                label = "Number of Sessions",
                choices = c("1 Session" = 1, "2 Sessions" = 2, 
                            "3 Sessions" = 3, "10 Sessions"= 10)
            ),
            
            conditionalPanel(
                condition = "input.numSessions == 1",
                fileInput(inputId = "s1", label = "s1 upload")
            ),
            
            conditionalPanel(
                condition = "input.numSessions == 2",
                fileInput(inputId = "s1", label = "s1 upload"),
                fileInput(inputId = "s2", label = "s2 upload")
            ),
            
            conditionalPanel(
                condition = "input.numSessions == 3",
                fileInput(inputId = "s1", label = "s1 upload"),
                fileInput(inputId = "s2", label = "s2 upload"),
                fileInput(inputId = "s3", label = "s3 upload"),
            ),
            
            conditionalPanel(
                condition = "input.numSessions == 10",
                fileInput(inputId = "s1", label = "s1 upload"),
                fileInput(inputId = "s2", label = "s2 upload"),
                fileInput(inputId = "s3", label = "s3 upload"),
                fileInput(inputId = "s4", label = "s4 upload"),
                fileInput(inputId = "s5", label = "s5 upload"),
                fileInput(inputId = "s6", label = "s6 upload"),
                fileInput(inputId = "s7", label = "s7 upload"),
                fileInput(inputId = "s8", label = "s8 upload"),
                fileInput(inputId = "s9", label = "s9 upload"),
                fileInput(inputId = "s10", label = "s10 upload")
            )
        ),
        mainPanel (
            
        )
    )
)


server <- function(input, output) {
    
}


shinyApp(ui = ui, server = server)
0 Answers
Related