【发布时间】:2021-05-25 17:26:45
【问题描述】:
我正在构建应用程序,用户可以在其中编辑数据表并单击按钮以反映此数据表的不可编辑副本中的更改(在最终项目中,我需要有两个数据集需要手动匹配),但现在这个小 MWE 显示了我在制作可以进行更改的反应表副本时遇到的问题,而无需更改原始反应表的数据。我想让这个应用程序工作,您单击编辑表 dat_joined$data/output$mytable 中的单元格,并且这些更改确实反映在新表 mydf$data/output$table2 中。最初(在进行任何更改之前)执行 mydf$data 需要是 dat_joined$data 的副本这是对这个问题和答案的跟进:how to make a copy of a reactive value in shiny server function
library(shiny)
library(DT)
library(shinyWidgets)
library(tidyverse)
# create master dataframe
dat_total <- tibble(ID_1 = 1:10, names = letters[1:10],
ID_2 = 11:20, names_2 = LETTERS[c(3:5, 1, 2, 6:8, 10, 9)])
shinyApp(
ui = fluidPage(
title = 'Radio button and a dropdown manue ',
sliderInput("n_rows_table", "Number of rows:",
min = 0, max = 10,
value = 5),
actionBttn(
inputId = "button_1",
label = "Make tables",
size = "sm",
color = "warning"
),
DT::dataTableOutput("mytable"),
actionBttn(
inputId = "button_2",
label = "Process",
size = "sm",
color = "success"),
DT::dataTableOutput("table2")),
server = function(input, output, session) {
# set up reactive values
dat_left <- reactiveValues(data=NULL)
dat_right <- reactiveValues(data=NULL)
dat_joined <- reactiveValues(data=NULL)
# create reactive daraframe
dat <- eventReactive(input$button_1, {
dat_total[1:input$n_rows_table, ] %>%
rowid_to_column()})
# Split the data into a right and a left set
observe({
dat_left$data <- dat() %>%
select(rowid, ID_1, names)
})
observe({
dat_right$data <- dat() %>%
select(rowid, ID_2, names_2,ID_1)
})
# join these again
# This is needed because my actual app will
# be used to manually match 2 datasets
observe({
if (is.null( dat_right$data )) {
NULL
}else{
dat_joined$data <- left_join(dat_left$data,
dat_right$data,
by = "rowid")
}
})
# Print the the datasets
output$mytable <- renderDT({
datatable(dat_joined$data ,
rownames = F,
editable = "cell")
})
# I want to make a copy of the dat_joined$data dataset into dat$mydf
# none of these function as expected
#mydf <- reactiveValues(data=isolate(dat_joined$data))
#mydf <- reactiveValues(data=local(dat_joined$data))
#mydf <- reactiveValues(data=dat_joined$data)
#mydf <- reactiveValues(data=NULL)
# This works, but only saves the cells to w
mydf <- reactiveValues(data=matrix(NA, nrow=10, ncol = 5))
# Ideally the computation only happens when this both an edit is made
# and the button is pressed (now I need to press it between every edit)
# validate_event <- reactive({
# req(input$mytable_cell_edit) & req(input$button_2)
# })
#observeEvent(input$button_2validate_event(), { DOes not work
observeEvent(input$button_2,{
info = input$mytable_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
mydf$data[i, j] <- DT::coerceValue(v, mydf$data[i, j])
})
# print
output[["table2"]] <- renderDT({
datatable(mydf$data)
})
}
)
【问题讨论】:
-
对于它的工作原理,
reactiveValues是一个类似列表的结构(注意复数 's')。它可以在单个对象中包含所有反应值,只需为条目赋予不同的名称。 -
您的问题到底出在哪里?
-
我想让这个应用程序正常工作,您可以在其中单击编辑表 dat_joined$data/output$mytable 中的单元格,并且这些更改确实反映在新表 mydf$data/output$table2 中.最初(在进行任何更改之前)执行 mydf$data 需要是 dat_joined$data 的副本
-
我不确定我是否完全理解您的问题。您是否希望仅在按下 button_2 时才看到表 1(顶部)中已编辑的更改反映在表 2(底部)中?
-
是的(这是一方面),但主要是我希望我在顶部表格中所做的更改反映在底部的表格中,同时也允许我(在应用程序的后期阶段) ) 对底部表格进行更改(删除行、重新排序列、将数据从
ID_1匹配到ID_2等),而这些更改不会反映在顶部的表格中。