我想在 Shiny 的模块中有一个可编辑的 DT。当我更改 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)
})
}
flexdashboard 中的应用程序:
```{r}
modDtUi("editable")
```
```{r}
callModule(modDt,"editable", data = iris)
```
它在没有模块的情况下运行良好,但我无法使用 Shiny 的模块获得相同的结果。
谢谢
最佳答案
如果您删除 rownames = FALSE
,则此方法有效:
replaceData(proxy, x, resetPaging = FALSE)#, rownames = FALSE)
如果你不想要行名,你还必须设置
rownames = FALSE
在 renderDataTable
: output$x1 <- DT::renderDataTable(x, selection = 'none', editable = TRUE,
rownames = FALSE)
然后你必须添加
1
至info$col
: observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
i = info$row
j = info$col + 1
v = info$value
x[i, j] <<- DT::coerceValue(v, x[i, j])
replaceData(proxy, x, resetPaging = FALSE, rownames = FALSE)
})
Rmd
的完整代码弹性仪表板:---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(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,
rownames = FALSE)
proxy <- dataTableProxy('x1', session = session)
observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
i = info$row
j = info$col + 1
v = info$value
x[i, j] <<- DT::coerceValue(v, x[i, j])
replaceData(proxy, x, resetPaging = FALSE, rownames = FALSE)
})
}
```
Column {data-width=650}
-----------------------------------------------------------------------
### Editable table
```{r}
modDtUi("editable")
```
```{r}
callModule(modDt, "editable", data = iris)
```
关于r - Shiny 模块内的可编辑 DT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56540986/