【发布时间】:2019-12-13 22:50:53
【问题描述】:
我正在尝试在服务器端编辑数据表(DT:datatable),并且我正在使用在前端呈现的反应式数据表。现在我想编辑数据表并从编辑的数据表中检索信息。这是一个可重复的示例,说明我在 reprex 中的 cmets 中提到的要实现的目标:
library(shiny)
library(DT)
library(tidyverse)
d <- iris
ui <- fluidPage(
dataTableOutput("table1"),
dataTableOutput("table2")
)
server <- function(input,output,session){
# This is the main table I would want to display in its full context
output$table1 <- renderDataTable(
datatable(d)
)
get_row <-reactive({
d %>% slice(input$table1_rows_selected)})
# Here the table with row selected from table 1 is displayed
output$table2 <- renderDataTable({
datatable(get_row(),
editable = TRUE)
})
# Now as the cell gets edited in table 2, i want the edited value to show and make the last column values = NA
proxy <- dataTableProxy("table2")
observeEvent(eventExpr = input$table2_cell_edit, handlerExpr = {
x <- isolate(get_row())
info = input$table2_cell_edit
i = info$row
j = info$column
v = info$value
x[i, j] <<- DT::coerceValue(v, x[i, j])
x <- x %>% mutate(Species = NA)
replaceData(proxy, x, resetPaging = FALSE)
})
}
shinyApp(ui = ui, server = server)
我收到一个错误
【问题讨论】:
-
嗨!欢迎来到堆栈溢出!这可能是一个简单的问题,但您更有可能通过可重现的最小示例获得帮助。我建议使用仅更新可编辑表(可能带有玩具数据)的完整应用程序(UI 和服务器)。说了这么多,如果你需要做你想做的那种分配,你可以考虑使用
reactiveValues()作为你的data对象。 -
嗨@RyanMorton 感谢您的回复。我已经更改了代码并提供了一个可重现的示例。让我知道您是否可以提供帮助