【问题标题】:In R Shiny, how to update only one value in a user input matrix?在 R Shiny 中,如何只更新用户输入矩阵中的一个值?
【发布时间】:2021-12-12 22:13:30
【问题描述】:

在下面的 MWE 代码中,我试图让矩阵 1 中的任何用户输入仅反应性地反映在矩阵 2 右上角 ([1,2)] 中,同时保留之前输入到矩阵 2 中的所有其他值. 底部的三张图片说明了问题:并非所有值都被准确保留。是否可以保留除 Matrix 2 [1,2] 之外的先前输入?如何做到这一点?

我已经摆弄了矩阵索引并且正在碰壁。

注意 R Studio 控制台中发布的警告,如最终图像所示。

这“几乎是 MWE”,但可以安全地忽略用于额外/插值的 UDF interpol(),从而简化代码。

MWE 代码:

library(shiny)
library(shinyMatrix)

interpol <- function(a, b) { # [a] = modeled periods, [b] = matrix inputs
  c <- b
  c[,1][c[,1] > a] <- a
  d <- diff(c[,1, drop = FALSE])
  d[d <= 0] <- NA
  d <- c(1,d)
  c <- cbind(c,d)
  c <- na.omit(c)
  c <- c[,-c(3),drop=FALSE]
  e <- rep(NA, a)
  e[c[,1]] <- c[,2]
  e[seq_len(min(c[,1])-1)] <- e[min(c[,1])]
  if(max(c[,1]) < a){e[seq(max(c[,1]) + 1, a, 1)] <- 0}
  e <- approx(seq_along(e)[!is.na(e)], e[!is.na(e)], seq_along(e))$y # Interpolates
  return(e)
}

ui <- fluidPage(
  sliderInput('periods', 'Modeled periods (X):', min=1, max=10, value=10),
  
  h5(strong("Matrix 1:")), 
  matrixInput("matrix1", 
              value = matrix(c(5), nrow = 1, ncol = 1, dimnames = list("Base rate (Y)",NULL)),
              rows =  list(extend = FALSE, names = TRUE),
              cols =  list(names = FALSE),
              class = "numeric"),
  
  h5(strong("Matrix 2:")), 
  matrixInput("matrix2",
              value = matrix(c(10,5), nrow = 1, ncol = 2, dimnames = list(NULL,c("X","Y"))),
              rows = list(extend = TRUE, names = TRUE, delete = TRUE),
              class = "numeric"),
  
  plotOutput("plot")
)

server <- function(input, output, session){
  
  observeEvent(input$periods,{
    updateMatrixInput(
      session, 
      inputId = "matrix2", 
      value = matrix(c(input$periods,input$matrix2[1,2]), 1, 2, dimnames = list(NULL,c("X","Y")))
    )
  })
  
  observeEvent(input$matrix1, {
    updateMatrixInput(
      session, 
      inputId = "matrix2",
      value = matrix(c(input$matrix2[,1],input$matrix1[,1]), ncol = 2, dimnames = list(NULL,c("X","Y")))
    )
  })
  
  observeEvent(input$matrix2, { 
    if(any(rownames(input$matrix2) == "")){
      tmpMatrix <- input$matrix2
      rownames(tmpMatrix) <- paste("Row", seq_len(nrow(input$matrix2)))
      isolate(updateMatrixInput(session, inputId = "matrix2", value = tmpMatrix))
      }
    input$matrix2
    })
  
  output$plot <- renderPlot({
    req(input$matrix2)
    plot(interpol(input$periods, input$matrix2))
  })
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r matrix shiny shiny-reactivity


    【解决方案1】:

    好的,这很容易。在 matrix1 的 observeEvent 中,我只是将 matrix2 的两列输入分解(到对象 col1col2),将 matrix1 值替换到组合 matrix2 列输入的向量中的正确位置(joinCol) ,并使用updateMatrixInput函数更新了matrix2,如下图:

    observeEvent(input$matrix1, {
      
        col1 <- input$matrix2[,1]
        col2 <- input$matrix2[,2]
        joinCol <- c(col1,col2)
        joinCol[length(input$matrix2)/2+1] <- input$matrix1[,1]
        
        updateMatrixInput(
          session, 
          inputId = "matrix2",
          value = matrix(joinCol,
                         ncol = 2, 
                         dimnames = list(NULL,c("X","Y")))
        )
      })
    

    【讨论】:

      猜你喜欢
      • 2021-11-22
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2015-07-23
      相关资源
      最近更新 更多