[英]Combining selectInput and DT::datatable editing in Shiny
I would like to update both a data.frame
and a DT::datatable
interactively when editing the datatable
cells.我想在编辑数据表单元格时以交互方式更新
data.frame
和DT::datatable
datatable
This works fine but when I use the selectInput
function to filter the data.frame
and edit cells in another row of the datatable
, it just copies the values I edited previously both in the data.frame
and datatable
.这工作正常,但是当我使用
selectInput
function 过滤data.frame
并编辑datatable
另一行中的单元格时,它只是复制我之前在data.frame
和datatable
中编辑的值。 Any suggestions?有什么建议么?
Below, is a reproducible example.下面是一个可重现的例子。 I guess that this is an issue of reactivity.
我想这是反应性的问题。 Being new to Shiny I am still far from mastering that.
作为 Shiny 的新手,我离掌握它还很远。
library(tidyverse); library(DT); library(shiny)
df <- data.frame(internal_idNew=c(1, 2, 3, 4), col_1=c("this", "is", "a", "column"))
ui <- fluidPage(
#filter df
selectInput("s_internal_idNew", "Record id (new)", choices=c(1:nrow(df))),
#dt output
dataTableOutput("dt")
)
server <- function(input, output) {
#reactive df
df <- reactiveVal({df})
#reactive df filtered
df_showed <- reactiveVal({})
observeEvent(input$s_internal_idNew, {
#filter a row matching the internal id
df_showed(df() %>% filter(internal_idNew==input$s_internal_idNew))
#render dt
output$dt <- DT::renderDataTable(df_showed(), editable=list(target = "cell", disable = list(columns =c(0))), options=list(dom = 't', bSort=FALSE, pageLength=1), rownames = FALSE, selection = "none")
#create proxy dt
dt_proxy <- dataTableProxy("dt")
#edit dt
observeEvent(input$dt_cell_edit, {
this <- df()
showed <- df_showed()
#extract edited value to edit df
col_name <- showed %>% names() %>% .[input$dt_cell_edit$col+1]
row_name <- input$s_internal_idNew %>% as.numeric()
value_name <- coerceValue(input$dt_cell_edit$value, showed[row_name, col_name])
#store edited values in reactive df
this[row_name, col_name] <- value_name
df(this)
#replace data in datatable
replaceData(dt_proxy, df_showed(), resetPaging = TRUE, rownames = FALSE)
})
})
}
shinyApp(ui = ui, server = server)
A few modifications to achieve expected behavior:一些修改以实现预期的行为:
dtProxy
should be created only once at server launch dtProxy
应该只在服务器启动时创建一次observeEvent(input$dt_cell_edit,...)
should be independent of observeEvent(input$s_internal_idNew,...)
observeEvent(input$dt_cell_edit,...)
应该独立于observeEvent(input$s_internal_idNew,...)
df_showed()
should also be updated, as df()
df_showed()
也应该更新,因为df()
library(tidyverse); library(DT); library(shiny)
df <- data.frame(internal_idNew=c(1, 2, 3, 4), col_1=c("this", "is", "a", "column"))
ui <- fluidPage(
#filter df
selectInput("s_internal_idNew", "Record id (new)", choices=c(1:nrow(df))),
#dt output
dataTableOutput("dt")
)
server <- function(input, output) {
#reactive df
df <- reactiveVal({df})
#reactive df filtered
df_showed <- reactiveVal({})
#create proxy dt once
dt_proxy <- dataTableProxy("dt")
observeEvent(input$s_internal_idNew, {
#filter a row matching the internal id
df_showed(df() %>% filter(internal_idNew==input$s_internal_idNew))
#render dt
output$dt <- DT::renderDataTable(df_showed(), editable=list(target = "cell", disable = list(columns =c(0))), options=list(dom = 't', bSort=FALSE, pageLength=1), rownames = FALSE, selection = "none")
})
#edit dt - separate from previous reactive
observeEvent(input$dt_cell_edit, {
this <- df()
showed <- df_showed()
#extract edited value to edit df
col_name <- showed %>% names() %>% .[input$dt_cell_edit$col+1]
row_name <- input$s_internal_idNew %>% as.numeric()
value_name <- coerceValue(input$dt_cell_edit$value, showed[row_name, col_name])
#store edited values in reactive df
this[row_name, col_name] <- value_name
df(this)
df_showed(this[row_name, ]) # Also updated
#replace data in datatable
replaceData(dt_proxy, df_showed(), resetPaging = TRUE, rownames = FALSE)
})
}
shinyApp(ui = ui, server = server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.