闪亮模块内可编辑的DT

Fra*_* VE 5 r shiny dt shinymodules

我想在闪亮的模块中有一个可编辑的设备标识符。当我更改 DT 中的值时,表会更新,并且数据表中的消息为空:

“未找到匹配的记录”

我的代码如下:

模块:

modDtUi <- function(id){ # UI module
  ns = NS(id)
  DT::dataTableOutput(ns('x1'))
} 


modDt <-  function(input, output, session, data){ # Server module

  x <- data
  output$x1 <- DT::renderDataTable(x, selection = 'none', editable = TRUE)

  proxy <- dataTableProxy('x1', session = session)

  observeEvent(input$x1_cell_edit, {
    info = input$x1_cell_edit
    str(info)
    print(info)
    i = info$row
    j = info$col
    v = info$value
    x[i, j] <<- DT::coerceValue(v, x[i, j])
    replaceData(proxy, x, resetPaging = FALSE, rownames = FALSE)
  })

}
Run Code Online (Sandbox Code Playgroud)

Flexdashboard 中的应用程序:

```{r}
modDtUi("editable")
```

```{r}
callModule(modDt,"editable", data = iris)
```
Run Code Online (Sandbox Code Playgroud)

它在没有模块的情况下运行良好,但我无法使用闪亮的模块获得相同的结果。

谢谢

asa*_*ica 2

根据您的代码,问题是代理需要全局会话(而不是模块会话)。请参阅我的其他答案以了解替代方法。

您可以简单地通过参数将全局传递session给模块。

这有效:

library(shiny)
library(DT)

modDtUi <- function(id){ # UI module
  ns = NS(id)
  DT::dataTableOutput(ns('x1'))
}


modDt <-  function(input, output, session, data, globalSession){ # Server module

  x <- data
  output$x1 <- DT::renderDataTable(x, selection = 'none', editable = TRUE)

  proxy <- dataTableProxy('x1', session = globalSession)

  observeEvent(input$x1_cell_edit, {
    info = input$x1_cell_edit
    str(info)
    print(info)
    i = info$row
    j = info$col
    v = info$value
    x[i, j] <<- DT::coerceValue(v, x[i, j])
    replaceData(proxy, x, resetPaging = FALSE)
  })

}
Run Code Online (Sandbox Code Playgroud)

您现在必须在模块调用中添加全局会话。

使用闪亮的应用程序:

ui <- fluidPage(
  modDtUi("editable")
)

server <- function(input, output, session) {
  callModule(modDt,"editable", data = iris, globalSession = session)
}

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

使用弹性仪表板:

```{r}
modDtUi("editable")
```

```{r}
callModule(modDt, "editable", data = iris, globalSession = session)
```
Run Code Online (Sandbox Code Playgroud)

如果您想在应用程序的其余部分使用更新后的表,只需reactive(x)从模块返回并在调用模块时捕获它即可。

```{r}
modDtUi("editable")
```

```{r}
callModule(modDt, "editable", data = iris, globalSession = session)
```
Run Code Online (Sandbox Code Playgroud)