How to add a “back to top of page” button in R Shiny?

狂风中的少年 提交于 2021-01-01 02:10:25

问题


This is a very common feature among other webapps, but in R Shiny, how do you add a button that brings the user back to the top of the page when clicked?

In addition to that, is it possible to set how far up or down the user's view moves to?

Many thanks, I searched for a while but can't find any posts about this.


回答1:


Using the shinyjs package, you can define a simple jump to top function along the lines of

jscode <- "shinyjs.toTop = function() {document.body.scrollTop = 0;}"

And you can then assign the code to a button in the ui

  useShinyjs(),
  extendShinyjs(text = jscode),
  actionButton("toTop", "jump to top")

And the have an observer in the server that executes the javascript when the button is clicked, something like

observeEvent(input$toTop, {
    js$toTop();
 })

Note that I have not tested this answer, but it should be enough to point you in the right direction (for example, the javascript might not work with different browsers)




回答2:


use the "gotop" package

available on CRAN

install it by calling install.package("gotop")

very simple to use, just add use_gotop() anywhere in the UI and you're good.



来源:https://stackoverflow.com/questions/44423716/how-to-add-a-back-to-top-of-page-button-in-r-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!