我想在一个闪亮的模块中有一个可编辑的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)
```
在没有模块的情况下效果很好,但是在闪亮的模块下我无法获得相同的结果。
谢谢
答案 0 :(得分:1)
如果您删除this.setState({ data: res.data }
,这将起作用:
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)
})
flexdashboard的完整代码:
Rmd
答案 1 :(得分:1)
根据您的代码工作,问题在于代理需要全局会话(而不是模块会话)。有关其他方法,请参见我的其他答案。
您只需通过参数将全局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)
})
}
您现在必须在模块调用中添加全局会话。
使用闪亮的应用程序:
ui <- fluidPage(
modDtUi("editable")
)
server <- function(input, output, session) {
callModule(modDt,"editable", data = iris, globalSession = session)
}
shinyApp(ui = ui, server = server)
使用flexdashboard:
```{r}
modDtUi("editable")
```
```{r}
callModule(modDt, "editable", data = iris, globalSession = session)
```
如果您想在应用程序的其余部分中使用更新后的表,只需从模块返回reactive(x)
并在调用模块时捕获它即可。
editable_iris <- callModule(modDt,"editable", data = iris, globalSession = session)
答案 2 :(得分:0)
以下内容为我提供了一个可编辑表,并以反应式形式捕获了编辑后的表,以供在应用程序中进一步使用:
library(shiny)
library(DT)
modDtUi <- function(id){ # UI module
ns = NS(id)
DT::dataTableOutput(ns('x1'))
}
modDt <- function(input, output, session, data){ # Server module
output$x1 <- DT::renderDataTable(data, selection = 'none', editable = TRUE, server = TRUE)
proxy <- dataTableProxy('x1', session = session)
updatedData <- eventReactive(input$x1_cell_edit, {
info = input$x1_cell_edit
if (!is.null(info)) {
str(info)
data[info$row, info$col] <<- DT::coerceValue(info$value,
data[info$row, info$col])
}
data
}, ignoreNULL = FALSE)
return(updatedData)
}
ui <- fluidPage(
modDtUi("editable"),
tags$hr(),
"Proof it works: the table below updates on edit.",
shiny::tableOutput("proof")
)
server <- function(input, output) {
editable_dt <- callModule(modDt,"editable", data = iris)
output$proof <- renderTable({
editable_dt() %>%
summarise_if(is.numeric, mean)
})
}
shinyApp(ui = ui, server = server)