r - 退出闪亮的应用程序时添加“您确定要离开此页面”警报消息
问题描述
是否可以添加“您确定要离开此页面吗?” 当浏览器即将关闭时,向闪亮的应用程序退出消息?
不幸的是,我在 StackExchange 的其他地方找不到答案。我认为您可以使用 javascript 来执行此操作,尽管我对它不是很熟悉。提前非常感谢。
解决方案
实现这一点的最简单方法是window.onbeforeunload
在 JavaScript 中使用。
另一个Stackoverflow 主题中接受的答案的代码:
window.onbeforeunload = function() {
return 'Your changes will be lost!';
};
fluidPage
您可以通过将其包含在Shiny 应用程序或dashboardBody
Shiny 仪表板页面中来实现此(以及任何 JavaScript 代码) 。
JavaScript 代码需要包含在 head 和 script 标签中(与在 HTML 中包含 JS 文件时相同)
解决方案:
tags$head(tags$script(HTML("
// Enable navigation prompt
window.onbeforeunload = function() {
return 'Your changes will be lost!';
};
"))),
默认 Geyser 应用程序的可重现示例:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Navigation prompt
tags$head(tags$script(HTML("
// Enable navigation prompt
window.onbeforeunload = function() {
return 'Your changes will be lost!';
};
"))),
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
# Run the application
shinyApp(ui = ui, server = server)
推荐阅读
- linux - Azure Pipeline Agent 无法使用代理并出现 Curl 错误:CURLE_RECV_ERROR
- javascript - 使用redux登录后如何在反应路由器dom中重定向到主页?
- scala - 如何将火花数据框转换为 SQL 查询?
- c# - DbUpdateConcurrencyException 在尝试从 DbSet 中删除条目而不先加载它时发生删除
- java - 将数组分成3块并将它们放入数组数组中?
- java - 简单的 java 布尔值。为什么这会返回假?
- mysql - 快速估计 MySQL 中查询扫描的行数
- reactjs - 跨域读取阻塞 (CORB) 阻止了 React 应用程序上的跨域响应
- apache - 如何在浏览器上为 xampp htdocs 项目启用简单名称
- hibernate - Hibernate 级联顺序、ID 生成和双向关系