【问题标题】:how to close the connection when exiting the shiny app退出闪亮的应用程序时如何关闭连接
【发布时间】:2021-02-01 15:09:37
【问题描述】:

当我退出(停止)我的闪亮应用程序时,我使用 websocket 连接制作闪亮的应用程序,并希望关闭连接。我有一个建议功能 on.exit() 可以解决我的问题,但我不知道在闪亮的应用程序中在哪里使用它。有没有其他方法可以在应用停止时关闭连接?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以为此目的使用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)
    
    

    【讨论】:

      猜你喜欢
      • 2015-11-17
      • 2017-07-05
      • 2015-02-06
      • 1970-01-01
      • 2015-08-08
      • 2017-04-29
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多