Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny Dashboard Scroll to Top on Button Click

Is there any way to scroll to the top of the page in ShinyDashboard on a button click?

I've added the following lines in ui.R under dashboardSidebar( :

useShinyjs(), 
extendShinyjs(text = "shinyjs.button = function() {document.body.scrollTop = 0;}"),

and the following in server.R under observeEvent(input$button, {:

js$button()

This seems to work in the app preview in RStudio, but not in browser.

like image 407
HGupta Avatar asked Mar 07 '23 17:03

HGupta


1 Answers

Using the first Google hit for "javascript scroll to top", the most upvoted answer is window.scrollTo(x-coord, y-coord);

I tried replacing your document.body.scrollTop = 0; with window.scrollTo(0, 0) and it worked.

FYI, if you only have that one very simple function to define, you can also get away with shinyjs::runjs("window.scrollTo(0, 50)") in the server code

like image 148
DeanAttali Avatar answered Mar 16 '23 00:03

DeanAttali