在R Shiny App中禁用浏览器的后退按钮

SBi*_*sta 1 shiny shinydashboard

我正在构建一个有很多条件面板的闪亮应用程序。我在应用程序本身中有一个后退按钮,可以在条件面板之间导航。我想禁用Web浏览器的后退按钮,因为单击该按钮会转到上一个网页(远离我的应用程序)。有没有办法做到这一点?

Por*_*hop 5

您可以编写一些代码javascript来做到这一点。这里有两个示例,请注意,我仅在Chrome

示例1back在浏览器中激活按钮后返回一条消息

rm(list = ls())
library(shiny)
jscode <- 'window.onbeforeunload = function() { return "Please use the button on the webpage"; };'
ui <- basicPage(
  mainPanel(tags$head(tags$script(jscode)))
)

server <- function(input, output,session) {}
runApp(list(ui = ui, server = server))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

示例2将完全禁用导航。我个人不喜欢这种方法,因为人们可能会因为您的网站不提供标准的导航功能而感到恼火

rm(list = ls())
library(shiny)
jscode2 <- "history.pushState(null, null, document.title);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.title);});"
ui <- basicPage(
  mainPanel(tags$head(tags$script(jscode2)))
)

server <- function(input, output,session) {}
runApp(list(ui = ui, server = server))
Run Code Online (Sandbox Code Playgroud)