有多少用户连接到我闪亮的应用程序?

时间:2021-01-31 19:35:00

I work on a Shiny application with shinydashboard and somewhere in the app I want to show a notification telling the user how many other users are connected to the application at the same time.

我使用shinydashboard开发了一个闪亮的应用程序,我想在应用程序的某个地方显示一个通知,告诉用户同时有多少其他用户连接到应用程序。

I came up with a first piece of code that seems to work:

我想到了第一段似乎有用的代码:

library(shiny)
ui=fluidPage(uiOutput("text"))
number.users <- 0 # global variable
server= function(input, output, session){
     already.counted = FALSE
     autoInvalidate = reactiveTimer(2000)
     observe({
          if (!already.counted) {
               already.counted <- TRUE
               number.users <<- number.users + 1
          }
     })
     output$text = renderUI({
          autoInvalidate()
          h1(paste0("There are ", number.users, " user(s) connected to this app"))
          })
     onSessionEnded(function(){number.users <<- number.users - 1})
}

However it's not very "clean" and I'm afraid that my call to observe and the invalidate every 2 secs are adding some useless computing time to my (already heavy) app. I was wondering if there was some kind of function in shiny that triggers an event at the start of each new session, something like onsessionstart() that I could use directly to increment the global variable number.users, and a way to notify other sessions that a new session has been opened? Or is there any simpler way to go?

然而不是很“干净”,恐怕我的电话去观察和无效每2秒是添加一些无用的计算时间(已经沉重的)应用程序。我想知道如果有某种功能的闪亮的触发事件的每一个新会话,类似onsessionstart(),我可以直接使用全局变量数量增加。用户,以及通知其他会话已打开新会话的方法?或者有更简单的方法吗?

Thanks

谢谢

1 个解决方案

#1


2  

The root server function is called once for every new session, and effectively serves as the "onSessionStart" callback. I think what you're doing is fine, except the observe isn't really necessary.

根服务器函数对每个新会话调用一次,有效地充当“onSessionStart”回调。我认为你所做的一切都很好,只是观察并不是必要的。

To share global state between sessions in a more reactive manner, you can initialize a reactiveVal or reactiveValues object in global scope (outside of a reactive context), and then take a reactive dependency on it within each session. Any updates to these reactive values will immediately propagate to all other sessions.

要以更被动的方式在会话之间共享全局状态,您可以在全局作用域中(在反应上下文之外)初始化reactiveVal或reactiveValues对象,然后在每个会话中对其进行反应性依赖。对这些反应性值的任何更新将立即传播到所有其他会话。

library(shiny)

users = reactiveValues(count = 0)

ui = fluidPage(uiOutput("text"))

server = function(input, output, session) {
  onSessionStart = isolate({
    users$count = users$count + 1
  })

  onSessionEnded(function() {
    isolate({
      users$count = users$count - 1
    })
  })

  output$text = renderUI({
    h1(paste0("There are ", users$count, " user(s) connected to this app"))
  })
}

shinyApp(ui, server)

Btw, a more complex example of inter-session communication, though maybe a bit old now, is the Shiny chat room app - http://shiny.rstudio.com/gallery/chat-room.html

顺便说一句,会话间通信的一个更复杂的例子是闪亮的聊天室应用http://shiny.rstudio.com/gallery/chat-room.html

#1


2  

The root server function is called once for every new session, and effectively serves as the "onSessionStart" callback. I think what you're doing is fine, except the observe isn't really necessary.

根服务器函数对每个新会话调用一次,有效地充当“onSessionStart”回调。我认为你所做的一切都很好,只是观察并不是必要的。

To share global state between sessions in a more reactive manner, you can initialize a reactiveVal or reactiveValues object in global scope (outside of a reactive context), and then take a reactive dependency on it within each session. Any updates to these reactive values will immediately propagate to all other sessions.

要以更被动的方式在会话之间共享全局状态,您可以在全局作用域中(在反应上下文之外)初始化reactiveVal或reactiveValues对象,然后在每个会话中对其进行反应性依赖。对这些反应性值的任何更新将立即传播到所有其他会话。

library(shiny)

users = reactiveValues(count = 0)

ui = fluidPage(uiOutput("text"))

server = function(input, output, session) {
  onSessionStart = isolate({
    users$count = users$count + 1
  })

  onSessionEnded(function() {
    isolate({
      users$count = users$count - 1
    })
  })

  output$text = renderUI({
    h1(paste0("There are ", users$count, " user(s) connected to this app"))
  })
}

shinyApp(ui, server)

Btw, a more complex example of inter-session communication, though maybe a bit old now, is the Shiny chat room app - http://shiny.rstudio.com/gallery/chat-room.html

顺便说一句,会话间通信的一个更复杂的例子是闪亮的聊天室应用http://shiny.rstudio.com/gallery/chat-room.html