r - 退出闪亮的应用程序时如何关闭连接
问题描述
当我退出(停止)我的闪亮应用程序时,我使用 websocket 连接制作闪亮的应用程序,并希望关闭连接。我有一个建议,函数on.exit()可以解决我的问题,但我不知道在闪亮的应用程序中在哪里使用它。当应用程序停止时,还有其他方法可以关闭连接吗?
解决方案
您可以session$onSessionEnded
用于此目的。一旦会话结束,里面的代码就会运行。例如:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# 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')
})
session$onSessionEnded(function() {
# you can put your code here to close the connection
})
}
# Run the application
shinyApp(ui = ui, server = server)
推荐阅读
- ios - 点击屏幕会导致当前控制器返回到前一个控制器
- android - 如何解决cordova应用程序突然崩溃的问题?
- apache - 使用 VirtualHost 将 HTTP 重定向到 HTTPS
- yaml - Jekyll 默认(布局) Front Matter 不起作用:已修复
- python - 创建循环以通过多个 URL 导入数据
- updates - 错误:无法验证更新。确保网络安全,然后重试。
- javascript - 找不到模块:错误:无法解析“croppie”
- sql-server - 查询和子查询遇到问题:SQL 子查询返回超过 1 个值
- c# - CookieAuthenticationOptions.LogoutPath 属性在 ASP.NET Core 2.1 中有什么作用?
- vue.js - Vue.js/Vuex 登录:[vuex] 未知操作类型:postLogin。