【问题标题】:reactive environment within reactiveValues (RShiny)reactiveValues 中的反应性环境(R Shiny)
【发布时间】:2020-07-07 06:33:21
【问题描述】:

我正在尝试使用要使用 observeEvent 表达式更新的反应维度构建矩阵。我的想法如下:

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

我理解这个问题:在 mat = ... 我不能使用另一个反应式表达式,但是我没有替代解决方案,非常感谢任何关于此的输入。

提前致谢!

亲切的问候,

朱利安


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)
  )

  ### Create reactiveValues matrix with dimensions specified in length
  beta.matrix <-  reactiveValues(
    mat = matrix(0, input$length, input$length)
  )

  ### 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)

【问题讨论】:

    标签: shiny reactive


    【解决方案1】:

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

    1. 当维度改变时初始化一个空矩阵
    2. 对矩阵内容的变化做出反应

    请检查以下内容:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 2014-12-14
      • 1970-01-01
      • 2021-11-14
      相关资源
      最近更新 更多