反应值内的反应环境(RShiny)

反应值内的反应环境(RShiny),shiny,reactive,Shiny,Reactive,我正在尝试构建一个带有反应维度的矩阵,我希望使用observeEvent表达式更新该矩阵。我的想法如下: library(shiny) ui <- fluidPage( numericInput("length", "Dimensions of the matrix", value = 5), numericInput("a", "value for a", value = 2), numericInput("b", "value for b", value = 2),

我正在尝试构建一个带有反应维度的矩阵,我希望使用observeEvent表达式更新该矩阵。我的想法如下:

library(shiny)

ui <- fluidPage(
  numericInput("length", "Dimensions of the matrix", value = 5),
  numericInput("a", "value for a", value = 2),
  numericInput("b", "value for b", value = 2),
  numericInput("ind1", "value for index vector 1", value = 1),
  numericInput("ind2", "value for index vector 2", value = 1),
  actionButton("go", "Update"),
  tableOutput("matrix")
)

server <- function(input, output) {
  ### Calculate the value that will be used for the update
  value <- reactive(mean(rbeta(100, input$a, input$b)))

  ### Create a reactive index vector used to determine the position of the cell in the matrix
  ind <- reactive(c(input$ind1, input$ind2))

  beta.matrix <- reactiveValues(mat = NULL)
  beta.matrix.ini <- reactive({
    mat = matrix(0, input$length, input$length)
  })

  observe({
    beta.matrix$mat <- beta.matrix.ini()
  })

  ### Update matrix at positon ind with new value
  observeEvent(input$go, {
    beta.matrix$mat[ind()[1], ind()[2]] <- value()
  })

  ### Render matrix
  output$matrix <- renderTable({
    mat <- beta.matrix$mat
    mat
  })

}

# Run the application
shinyApp(ui = ui, server = server)
首先,我创建了一个reactiveValues对象,它有一个维度矩阵,输入$length(->reactive)和输入0。然后我使用observeEvent和actionButton来触发矩阵中的更新。这需要使用反应值(value())更新矩阵中由反应索引向量(ind())表示的特定单元格

我理解这个问题:在mat=。。。我不能使用另一个反应式表达式,但我没有其他解决方案,非常感谢您对此的任何意见

提前谢谢

亲切问候,

朱利安



ui我认为您需要两个反应性“阶段”

  • 当尺寸更改时,初始化空矩阵
  • 对矩阵内容的更改作出反应
  • 请检查以下内容:

    library(shiny)
    
    ui <- fluidPage(
      numericInput("length", "Dimensions of the matrix", value = 5),
      numericInput("a", "value for a", value = 2),
      numericInput("b", "value for b", value = 2),
      numericInput("ind1", "value for index vector 1", value = 1),
      numericInput("ind2", "value for index vector 2", value = 1),
      actionButton("go", "Update"),
      tableOutput("matrix")
    )
    
    server <- function(input, output) {
      ### Calculate the value that will be used for the update
      value <- reactive(mean(rbeta(100, input$a, input$b)))
    
      ### Create a reactive index vector used to determine the position of the cell in the matrix
      ind <- reactive(c(input$ind1, input$ind2))
    
      beta.matrix <- reactiveValues(mat = NULL)
      beta.matrix.ini <- reactive({
        mat = matrix(0, input$length, input$length)
      })
    
      observe({
        beta.matrix$mat <- beta.matrix.ini()
      })
    
      ### Update matrix at positon ind with new value
      observeEvent(input$go, {
        beta.matrix$mat[ind()[1], ind()[2]] <- value()
      })
    
      ### Render matrix
      output$matrix <- renderTable({
        mat <- beta.matrix$mat
        mat
      })
    
    }
    
    # Run the application
    shinyApp(ui = ui, server = server)
    
    库(闪亮)
    
    ui我认为这里需要两个反应“阶段”

  • 当尺寸更改时,初始化空矩阵
  • 对矩阵内容的更改作出反应
  • 请检查以下内容:

    library(shiny)
    
    ui <- fluidPage(
      numericInput("length", "Dimensions of the matrix", value = 5),
      numericInput("a", "value for a", value = 2),
      numericInput("b", "value for b", value = 2),
      numericInput("ind1", "value for index vector 1", value = 1),
      numericInput("ind2", "value for index vector 2", value = 1),
      actionButton("go", "Update"),
      tableOutput("matrix")
    )
    
    server <- function(input, output) {
      ### Calculate the value that will be used for the update
      value <- reactive(mean(rbeta(100, input$a, input$b)))
    
      ### Create a reactive index vector used to determine the position of the cell in the matrix
      ind <- reactive(c(input$ind1, input$ind2))
    
      beta.matrix <- reactiveValues(mat = NULL)
      beta.matrix.ini <- reactive({
        mat = matrix(0, input$length, input$length)
      })
    
      observe({
        beta.matrix$mat <- beta.matrix.ini()
      })
    
      ### Update matrix at positon ind with new value
      observeEvent(input$go, {
        beta.matrix$mat[ind()[1], ind()[2]] <- value()
      })
    
      ### Render matrix
      output$matrix <- renderTable({
        mat <- beta.matrix$mat
        mat
      })
    
    }
    
    # Run the application
    shinyApp(ui = ui, server = server)
    
    库(闪亮)
    用户界面