R Shiny中observeEvent(input$matrix2)导致matrix1监听失效问题求解
问题根因
- 同步触发逻辑缺失:原代码仅在
input$matrix2发生变化时,才会将input$matrix1的取值同步到matrix2的「Scenario 1」列,没有对input$matrix1的变化做监听。当用户提前点击按钮打开模态框生成input$matrix2后,plotData响应式变量会永久进入读取input$matrix2的分支,后续matrix1的修改不会同步到matrix2,因此图表不会更新,看起来就像matrix1失效。 - 循环触发隐患:原
observeEvent(input$matrix2, {...})内部调用updateMatrixInput修改matrix2的值,会再次触发该观察者,产生不必要的循环执行,额外消耗性能。
修复方案
仅需修改监听matrix2的观察者逻辑,将触发事件改为同时监听matrix1和matrix2的变化,同时用isolate包裹对input$matrix2的读取操作,避免循环触发即可。修改后的完整服务端代码如下:
server <- function(input, output, session){ observeEvent(input$matrix1, { tmpMat1 <- input$matrix1 if(any(rownames(input$matrix1) == "")){rownames(tmpMat1) <- paste("Row", seq_len(nrow(input$matrix1)))} updateMatrixInput(session, inputId = "matrix1", value = tmpMat1) }) # 此处为修改后的观察者逻辑 observeEvent(c(input$matrix1, input$matrix2), { req(input$matrix1, input$matrix2) # 用isolate包裹input$matrix2读取,避免updateMatrixInput触发本观察者循环 input_mat2 <- isolate(input$matrix2) a <- apply(input_mat2,2,'length<-',max(nrow(input_mat2),nrow(input$matrix1))) b <- apply(input$matrix1,2,'length<-',max(nrow(input_mat2),nrow(input$matrix1))) c <- if(length(a) == 2){c(b)} else {c(b,a[,-1:-2])} d <- ncol(input_mat2) tmpMat2 <- matrix(c(c), ncol = d) tmpMat2[1,2] <- input$matrix1[1,2] colnames(tmpMat2) <- paste("Scenario",rep(1:ncol(tmpMat2),each=2,length.out=ncol(tmpMat2))) rownames(tmpMat2) <- paste("Row", seq_len(nrow(input_mat2))) updateMatrixInput(session,inputId="matrix2",value=tmpMat2) }) observeEvent(input$matrix2show,{ showModal( modalDialog( matrixInput("matrix2", label = "Matrix 2 (Value Y applied in Period X):", value = if(is.null(input$matrix2)) {matrix(c(input$matrix1[,1],input$matrix1[,2]), ncol = 2, dimnames = list(NULL, rep("Scenario 1", 2)))} else {input$matrix2}, rows = list(extend = TRUE, delete = TRUE), cols = list(extend = TRUE, delta = 2, delete = TRUE, multiheader = TRUE), class = "numeric"), footer = tagList(modalButton("Exit box")) )) }) plotData <- reactive({ tryCatch( if(isTruthy(input$matrix2)){ lapply(seq_len(ncol(input$matrix2)/2), # column counter to set matrix index as it expands function(i){ tibble(Scenario = colnames(input$matrix2)[i*2-1], X = seq_len(10),Y = sumMat(input$matrix2[,(i*2-1):(i*2), drop = FALSE])) }) %>% bind_rows() } else {tibble(Scenario = "Scenario 1", X = seq_len(10),Y = sumMat(input$matrix1))}, error = function(e) NULL) }) output$plot <- renderPlot({ req(plotData()) plotData() %>% ggplot() + geom_line(aes(x = X, y = Y, colour = as.factor(Scenario))) }) }
修复后无论先操作matrix1还是先打开模态框查看matrix2,matrix1的修改都会自动同步到matrix2的「Scenario 1」,用户自行新增的其他场景也会正常保留。
内容的提问来源于stack exchange,提问作者Curious Jorge - user9788072
相关产品推荐
相关产品推荐

