繁体   English   中英

在通过 kubernetes 部署的 shiny 应用程序上使用 `server=FALSE` 时使用 `DT:replaceData()` 的替代方法

[英]Alternatives to using `DT:replaceData()` when `server=FALSE` on shiny application deployed via kubernetes

由于各种原因,我希望能够在使用客户端处理时使用代理数据表和 replaceData,即DT::renderDataTable(..., server = FALSE)

语境

我有一个 shiny 应用程序/仪表板,它与数据库通信并向用户提供信息。 用户可以在应用程序中填写表格,该表格将添加到数据库中,然后 shiny 应用程序通过查询数据库来更新数据以获取新信息。

该应用程序目前正在通过 kubernetes 使用LoadBalancer进行部署,目的是根据需要使用多个副本来扩展应用程序。 该应用程序没有通过 Shinyproxy 运行。

注意事项

目前,当应用程序由单个副本(进程)运行时,应用程序将表现得非常好并且能够使用server=TRUE 但是,当我增加要运行的进程/副本的数量时,除非在renderDataTable中指定了server=FALSE ,否则数据将无法呈现给用户。 由于目前未知的原因,但我怀疑这可能是由于会话不粘到 IP

虽然代码能够在server = TRUE时执行 function 很好,但如果我想允许多个用户应用它们都不能共享一个进程,因为一旦建立多个连接,应用程序将变得非常慢。 因此,我可能需要使用server=FALSE以便每个用户都能够以非常重要的功能细节为代价查看数据( replaceData停止工作)。 应用程序的产品所有者坚持认为这种行为保持不变,因为存在的数据通常很大,并且需要一些列排序和分页才能找到您想要查看的信息。 并且在提交表单时,如果我不使用replaceData并从头开始重建表,用户以前的表 state 就会丢失。

所以虽然我可以拆除数据表并在observeEvent中重新生成它

observeEvent(input$button, {
    ...
    output$table = renderDataTable({DT::datatable(df(), selection = 'single', callback = 
    JS("$.fn.dataTable.ext.errMode = 'none';"))}, server = FALSE)
    ...
})

这将提供一种解决方案,即使它会相应地更新表,也会产生不利的行为。

可重现的例子

这将创建一个带有按钮和表格的应用程序。 Select 在表格上一行然后点击按钮。 预期的行为是表格在所选行上更新为“new_content”。 这仅在server=TRUE时有效,在server=FALSE时不会发生任何事情。

library(shiny)
library(DT)
data(iris)

server <- function(input, output, session) {
  iris$new_col = ''
  df = reactive({iris})
  output$table = renderDataTable({
      DT::datatable(df(), selection = 'single', 
        callback = JS("$.fn.dataTable.ext.errMode = 'none';"))}, server = FALSE) # When TRUE code works fine,,,
  proxy = dataTableProxy('table')

  observeEvent(input$button, {
    # This line would be replacing the write to a db
    iris[input$table_rows_selected, 'new_col'] <- 'changed'
    # This line would be replacing the query to the db to reflect changes the user (and potentially other users have made between loading the data previously.
    df <- reactive({iris})
    proxy %>% replaceData(df(), rownames = TRUE, resetPaging = FALSE)
  })
}
    
ui <- fluidPage(
  actionButton('button', 'Press Me'),
  DT::DTOutput('table') 
)

shinyApp(ui, server)

我已经对 SO 进行了相当广泛的搜索,这是我能找到的最接近的问题: DT Editing in Shiny application with client-side processing (server = F) throws JSON Error但是这实际上并没有得到回答,并提供了“它只是不起作用”。

kubernetes.yaml(如果您是巫师,请只看)

我包括 yaml 文件,以防有一些 kubernetes boffins 知道如何通过一些巧妙的技巧专门解决上述问题。 所描述的问题可能源于在副本之间交换会话,因此数据被错误传达,但老实说,我在 kubernetes 方面并不是最好的......如果是这种情况,那么我将能够在 shiny 应用程序中使用 server=TRUE ,然后这个也可以解决问题。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-appname
spec:
  replicas: 5
  selector:
    matchLabels:
      app: appname
  template:
    metadata:
      labels:
        app: appname
    spec:
      containers:
      - name: appname 
        securityContext:
            privileged: false
        image: appname:latest
        ports: 
        - name: http
          containerPort: 3838
---
apiVersion: v1
kind: Service
metadata:
  name: servive-appname
spec:
  ports:
  - name: http
    port: 3838
    protocol: TCP
    targetPort: 3838
  selector:
    app: appname
  type: LoadBalancer
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-appname
  annotations:
    nginx.org/websocket-services: "service-appname"
spec:
  tls:
  - hosts:
    - appname.url.com
  rules:
  - host: appname.url.com
    http:
      paths:
      - path: /
        backend:
          serviceName: service-appname
          servicePort: 3838

我们可以尝试结合input$table_rows_selected的信息使用reactiveValues 根据请求, server参数等于FALSE

library(shiny)
library(DT)
data(iris)

server <- function(input, output, session) {
  iris$new_col = ''
  df = reactiveValues(iris = iris)
  
  
  
  output$table = renderDataTable({
    DT::datatable(df$iris, selection = 'single', 
                  callback = JS("$.fn.dataTable.ext.errMode = 'none';"))}, server = FALSE) # When TRUE code works fine,,,
  
  
  
  observeEvent(input$button, {
    
    # This line would be replacing the write to a db
    df$iris[input$table_rows_selected, c('new_col')] <- 'changed!'
    
  })
}

ui <- fluidPage(
  actionButton('button', 'Press Me'),
  DT::DTOutput('table') 
)

shinyApp(ui, server)

在此处输入图像描述

这是一种客户端方法,基于@jpdugo17 的答案和@TJGorrie 的初始示例,使用stateSave选项在重新渲染时维护表 state。 selectPageupdateSearch可以与dataTableProxy一起使用 - input$table_state$order的 state 需要作为选项传递:

library(shiny)
library(DT)
data(iris)

iris$new_col <- ''

server <- function(input, output, session) {
  
  DF = reactiveValues(iris = iris)
  
  output$table <- DT::renderDataTable(expr = {
    if (is.null(isolate(input$table_state))) {
      DT::datatable(
        DF$iris,
        selection = 'single',
        callback = JS("$.fn.dataTable.ext.errMode = 'none';"),
        options = list(stateSave = TRUE)
      )
    } else {
      # print(isolate(input$table_state$order))
      DT::datatable(
        DF$iris,
        selection = 'single',
        callback = JS("$.fn.dataTable.ext.errMode = 'none';"),
        options = list(
          stateSave = TRUE,
          order = isolate(input$table_state$order),
          paging = TRUE,
          pageLength = isolate(input$table_state$length)
        )
      )
    }
  }, server = FALSE)
  
  proxy <- dataTableProxy('table')
  
  observeEvent(input$button, {
    DF$iris[input$table_rows_selected, c('new_col')] <- 'changed!'
  })

  observeEvent(DF$iris, {
    selectPage(proxy, page = input$table_state$start/input$table_state$length+1)
    updateSearch(proxy, keywords = list(global = input$table_state$search$search, columns = NULL)) # see input$table_state$columns if needed
  }, ignoreInit = TRUE, priority = -1)
}

ui <- fluidPage(
  actionButton('button', 'Press Me'),
  DT::DTOutput('table') 
)

shinyApp(ui, server)

这是一篇相关文章

如果您使用的是 kubernetes/ingress-nginx,则可以使用 cookies 实现 session 亲和力。

https://kubernetes.github.io/ingress-nginx/examples/affinity/cookie/

但是从您的 yaml 中,您正在使用 nginx.org 的 kubernetes-ingress,然后您可以阅读

https://github.com/nginxinc/kubernetes-ingress/blob/master/examples/session-persistence/README.md

但仅在 NGINX Plus 中支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM