用户编辑一个 table,更改反映在另一个 table
User edit one table and the change is reflected in another table
我遇到了这个问题,我能找到的最接近的问题是参考此处提交的内容,但它并没有完全解决我要解决的问题
参考上面link中的更正示例,如果我希望能够编辑table a(第x_2列中的单元格)怎么办,这将自动更新table c(x_2列对应的单元格)。
谢谢
这里有一个更简单的版本,它不能使用代理(并使用新的模块接口),我希望它没问题。您可以更改前 2 个 table 中的任何值,第 3 个 table 显示总和并进行更新。诀窍是您编辑数据的模块必须 return 编辑数据作为反应,这些在主服务器函数中保存为变量。基于这些数据更新的模块需要将这些变量作为反应性输入。
非常重要的是:
- return 数据需要 return 反应的模块,最简单的方法是
return(reactive({returnvalue}))
- 在服务器功能中,不得评估传递给模块的反应,例如你必须使用
my_reactive_value
而不是 my_reactive_value()
### Libraries
library(shiny)
library(dplyr)
library(DT)
### Data----------------------------------------
set.seed(4)
table_a <- data.frame(
id=seq(from=1,to=10),
x_1=rnorm(n=10,mean=0,sd=10),
x_2=rnorm(n=10,mean=0,sd=10),
x_3=rnorm(n=10,mean=0,sd=10)
) %>%
mutate_all(round,3)
table_b <- data.frame(
id=seq(from=1,to=10),
x_1=rnorm(n=10,mean=0,sd=10),
x_2=rnorm(n=10,mean=0,sd=10),
x_3=rnorm(n=10,mean=0,sd=10)
)%>%
mutate_all(round,3)
mod_table_edit <- function(id, data_initialisation) {
moduleServer(
id,
function(input, output, session) {
# initialise the reactive data object for the table
data <- reactiveValues(table = data_initialisation)
# render the table
output$table <- renderDT({
datatable(data$table,
editable = TRUE)
})
# update the underlying data
observeEvent(input$table_cell_edit, {
data$table <- editData(data$table, input$table_cell_edit)
})
# return the data as a reactive
return(reactive(data$table))
}
)
}
mod_table_add <- function(id, data_input_1, data_input_2) {
moduleServer(
id,
function(input, output, session) {
# do the calculations
data_table <- reactive({
data_input_1() + data_input_2()
})
# render the table
output$table <- renderDT({
datatable(data_table())
})
}
)
}
modFunctionUI <- function(id) {
ns <- NS(id)
DTOutput(ns("table"))
}
ui <- fluidPage(
fluidRow(
column(4,
modFunctionUI("table_1")),
column(4,
modFunctionUI("table_2")),
column(4,
modFunctionUI("table_3"))
)
)
server <- function(input, output, session) {
# call the modules for the editable tables and store the results
data_table_1 <- mod_table_edit("table_1", data_initialisation = table_a)
data_table_2 <- mod_table_edit("table_2", data_initialisation = table_b)
# call the module for the table that takes inputs
# the reactives musn't be evaluated
mod_table_add("table_3",
data_input_1 = data_table_1,
data_input_2 = data_table_2)
}
shinyApp(ui, server)
我遇到了这个问题,我能找到的最接近的问题是参考此处提交的内容,但它并没有完全解决我要解决的问题
参考上面link中的更正示例,如果我希望能够编辑table a(第x_2列中的单元格)怎么办,这将自动更新table c(x_2列对应的单元格)。
谢谢
这里有一个更简单的版本,它不能使用代理(并使用新的模块接口),我希望它没问题。您可以更改前 2 个 table 中的任何值,第 3 个 table 显示总和并进行更新。诀窍是您编辑数据的模块必须 return 编辑数据作为反应,这些在主服务器函数中保存为变量。基于这些数据更新的模块需要将这些变量作为反应性输入。
非常重要的是:
- return 数据需要 return 反应的模块,最简单的方法是
return(reactive({returnvalue}))
- 在服务器功能中,不得评估传递给模块的反应,例如你必须使用
my_reactive_value
而不是my_reactive_value()
### Libraries
library(shiny)
library(dplyr)
library(DT)
### Data----------------------------------------
set.seed(4)
table_a <- data.frame(
id=seq(from=1,to=10),
x_1=rnorm(n=10,mean=0,sd=10),
x_2=rnorm(n=10,mean=0,sd=10),
x_3=rnorm(n=10,mean=0,sd=10)
) %>%
mutate_all(round,3)
table_b <- data.frame(
id=seq(from=1,to=10),
x_1=rnorm(n=10,mean=0,sd=10),
x_2=rnorm(n=10,mean=0,sd=10),
x_3=rnorm(n=10,mean=0,sd=10)
)%>%
mutate_all(round,3)
mod_table_edit <- function(id, data_initialisation) {
moduleServer(
id,
function(input, output, session) {
# initialise the reactive data object for the table
data <- reactiveValues(table = data_initialisation)
# render the table
output$table <- renderDT({
datatable(data$table,
editable = TRUE)
})
# update the underlying data
observeEvent(input$table_cell_edit, {
data$table <- editData(data$table, input$table_cell_edit)
})
# return the data as a reactive
return(reactive(data$table))
}
)
}
mod_table_add <- function(id, data_input_1, data_input_2) {
moduleServer(
id,
function(input, output, session) {
# do the calculations
data_table <- reactive({
data_input_1() + data_input_2()
})
# render the table
output$table <- renderDT({
datatable(data_table())
})
}
)
}
modFunctionUI <- function(id) {
ns <- NS(id)
DTOutput(ns("table"))
}
ui <- fluidPage(
fluidRow(
column(4,
modFunctionUI("table_1")),
column(4,
modFunctionUI("table_2")),
column(4,
modFunctionUI("table_3"))
)
)
server <- function(input, output, session) {
# call the modules for the editable tables and store the results
data_table_1 <- mod_table_edit("table_1", data_initialisation = table_a)
data_table_2 <- mod_table_edit("table_2", data_initialisation = table_b)
# call the module for the table that takes inputs
# the reactives musn't be evaluated
mod_table_add("table_3",
data_input_1 = data_table_1,
data_input_2 = data_table_2)
}
shinyApp(ui, server)