r - Shiny 模块内的可编辑 DT

标签 r shiny dt shinymodules

我想在 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 = FALSErenderDataTable :
  output$x1 <- DT::renderDataTable(x, selection = 'none', editable = TRUE, 
                                   rownames = FALSE)

然后你必须添加 1info$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/

相关文章:

r - 在 qplot 中使用带有数据框符号的列名

r - 如何在标签面板中并排放置多个图,并显示其他输出, Shiny 的 r?

r - Shiny 的无功输入和 "Go"按钮

r - 根据 plotly click 事件子集数据框

带有 html 标签的 R Shiny 数据表内容

r - 将数据帧的行作为参数传递给函数,同时保持其他参数不变

R 如何将边境国家添加到国家空间多边形 map

r - 如何在 R 中用不同颜色填充 geom_ribbon?

r - 如何在 Shiny 模块中使用 ShinyFiles 包 - 命名空间问题?

css - 如何在 DT::datatable 中禁用 scrollX